0

I am somewhat new to ASP.NET and I am confused by the syntax so I am a little lost. I am trying to hide/disable a button based on an if statement but I dont know how to disable or hide it. I have done C# before but this code looks unfamiliar to me.

Below is some of the code:

C# component:

  protected override void Render(HtmlTextWriter writer)
  {    
     string PostCtrl = Request.Params["__EVENTTARGET"];

     if (PostCtrl == "AccColLocation_content$collisionLocation$EditColLocation")
     {
          valDropDownlist(((CustomControl.DropDownValidator)collisionLocation.FindControl("valLoc_municipality")), "CollisionLocation.municipality");

            ..............    
     }    
  }

HTML:

 <ItemTemplate>
<asp:LinkButton ID="EditColLocation" runat="server" Text="Edit Collision Location" OnClick="CollisionLocation_Edit" />
 </ItemTemplate>

valDropDownList Method:

protected void valDropDownlist(CustomControl.DropDownValidator valDropdown, string DataElement)
{
    try
    {
        bool mvarRequired, srRequired;
        DataTable dtDataElement = DBFunctions.DBFunctions.getDataElement(RepDateTime, DataElement);
        string s = dtDataElement.Rows[0]["mvarRequired"].ToString();
        mvarRequired = (dtDataElement.Rows[0]["mvarRequired"].ToString() == "True") ? true : false;
        srRequired = (dtDataElement.Rows[0]["srRequired"].ToString() == "True") ? true : false;
        valDropdown.HaveToSelect = (SelfReported) ? srRequired : mvarRequired;
    }
    catch (Exception err)
    {
        MessageBox("An error occurred while setting drop down validation rules. " + err.ToString());
    }
}

All these buttons are in grid view.

I have something of this nature:

protected void deletedr(object sender, EventArgs e)
    {
        try
        {
            GridView gv = (GridView)FindControl("DriverInfo");
            gv.DataSource = DBFunctions.DBFunctions.getInfo(ReportID.Value, "", 2); ;
            gv.DataBind();

            bool isSelectedLast = false;
            DataTable dt = DBFunctions.DBFunctions.getInfo(ReportID.Value, "", 2);

            if (dlDriverNo.SelectedValue == dt.Rows[dt.Rows.Count - 1]["DriverNo"].ToString())
            {
                isSelectedLast = true;
            }

            if (!(DBFunctions.DBFunctions.deleteDriver(ReportID.Value, dlDriverNo.SelectedValue, isSelectedLast)))
            {
                MessageBox(null);
            }
            else
            {
                dlDriverNo.Visible = false;
                lblDelDriver.Visible = false;
                delDriverconfim.Visible = false;
                cancelDel.Visible = false;
                dlDriverNo.Items.Clear();
                gv.DataSource = DBFunctions.DBFunctions.getInfo(ReportID.Value, "", 2);
                gv.DataBind();
            }
        }
        catch (Exception err)
        {
            MessageBox("An error occurred while deleting the driver. " + err.ToString());
        }
    }
3
  • please provide code for method valDropDownlist Commented Jun 25, 2012 at 12:59
  • Where is the LinkButton located? In a GridView? FormView? Commented Jun 25, 2012 at 13:05
  • @user1429080 sorry for my noobyness with ASP.NET. The view seems to be GridView. Commented Jun 25, 2012 at 13:10

3 Answers 3

5

If your LinkButton is in a GridView, the most interesting problem is getting a handle on it. After you have a handle you can just set it to invisible or disabled:

linkButton.Visible = false;

or

linkButton.Enabled = false;

But to get a handle to the LinkButton control you will need to use .FindControl in a suitable event on the GridView control:

<asp:GridView ID="myGridView" runat="server" OnRowDataBound="myGridView_RowDataBound">
...
</aspGridView>

Then in the code behind you would have:

protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    var linkButton = (LinkButton)e.Row.FindControl("EditColLocation");
    if (linkButton != null) 
    {
        if (*your condition to check*)
            linkButton.Visible = false; 
    }
}

Hope this will get you moving in the right direction.

Sign up to request clarification or add additional context in comments.

9 Comments

I posted additional code that somehwhat relates to what you have.
@Nadal It's not clear from your question exactly WHEN you would like to disable/hide the LinkButton..?
I have special if statements that will do that job...what I need to know is HOW to disable that specific link button...if I do gridname.visible=false then the entire grid block disappears which isnt what I want...I only want that one linkbutton in the grid view to be hidden.
@Nadal What I'm after is a suitable server side event to use. Is the LinkButton supposed to be hidden after someone clicks on it? Or when someone clicks on some other button? Basically, what does the user do in the page that should cause the LinkButton to become hidden?
when the user clicks on a button it should be hidden
|
0

you can try

valDropdown.Visible = false; //Mask the control

4 Comments

Try EditColLocation.Visible = false;
I tried that and I got "The name 'EditColLocation' does not exist in the current context"....I put the object's ID, same happens with object name.
That's normal because your linkbutton exist on grid, you must find your linkbutton in item of grid. Use this code
LinkButton linkButton = (LinkButton)e.Item.FindControl("EditColLocation"); linkButton.Visible = false;
0

After the if condition write the below code. Buttonname.visible=false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.