LinkButton Event does not get called inside Gridview

1.2k Views Asked by At

I have table containing gridview inside an update panel "PanelSearch" whose display property is set to none.
When i first load the page i have added the table in a placeholder which is inside another updatepanel (main panel). I have a search button inside the "PanelSearch" and when user clicks search i am retrieving data from database and binding it to the gridview.

The gridview has linkbutton as templatefield and OnCommand event. When i click the linkbutton no event gets fired but my page refreshes and table inside "PanelSearch' disappears.

I don't know why.Can anyone please help me?

My markup is as below;

<asp:UpdatePanel ID="PanelPatientSearch" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <table id="tablePatientSearch" clientidmode="Static" cellpadding="2" cellspacing="2" border="0"
              style=" width:98%;display: table; margin-left: 10px;margin-right: 5px" runat="server" class="table">
              <tr>
              <th style="height:10px;width:100%"></th>
              </tr>
                <tr>
                    <td style="text-align: center">
                        <asp:Label ID="LabelSearchPatient" runat="server" CssClass="sectionHeaderForManagePages"
                            Text="Search Patient"></asp:Label>                          

                    </td>
                </tr>
                <tr>
                    <th style="height:10px;width:100%">
                        &nbsp;
                    </th>
                </tr>
                <tr>
                    <td>
                    <table cellpadding="5" cellspacing="3" width="100%" >
                    <tr> <th colspan="4">
                        &nbsp;
                    </th></tr>
                    <tr>
                    <th class="textCenterAlign" align="center" colspan="4"><asp:Label ID="labelSelectMessage" runat="server" Text="Search for Patient below or"></asp:Label> &nbsp;&nbsp&nbsp;
                    <asp:Button ID="buttonAddNewPatient" runat="server" CssClass="buttonLarge" 
                            Text="Add Patient" onclick="buttonAddNewPatient_Click" CausesValidation="false" />
                    </th>
                    </tr>
                      <tr> <th colspan="4">
                        &nbsp;
                    </th></tr>
                    <tr>
                    <td class="textRightAlign" width="25%">
                        <asp:Label ID="LabelPatientId" runat="server" Text="Patient ID" />
                    </td>
                    <td class="textLeftAlign" width="25%">
                        <asp:TextBox ID="textBoxID" runat="server" ClientIDMode="Static" CssClass="textBox" />
                        <asp:RequiredFieldValidator ID="RFVPatientID" runat="server" ControlToValidate="textBoxID"
                            CssClass="failureNotification" Display="None" ErrorMessage="Patient ID can not be blank"
                            SetFocusOnError="True" Width="0px"></asp:RequiredFieldValidator>
                        &nbsp;
                        <cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender1" runat="server" CssClass="ajaxvalidatorPopup"
                            Enabled="True" TargetControlID="RFVPatientID">
                        </cc1:ValidatorCalloutExtender>
                    </td>
                    <td class="textRightAlign" width="25%">
                        <asp:Label ID="labelFirstName" runat="server" Text="First Name" />
                    </td>
                    <td class="textLeftAlign" width="25%">
                        <asp:TextBox ID="textBoxFirstName" runat="server" CssClass="textBox" />
                    </td>
                    </tr>
                    <tr>
                    <td class="textRightAlign" width="25%">
                        <asp:Label ID="labelLastName" runat="server" Text="Last Name" />
                        <br />
                    </td>
                    <td class="textLeftAlign" width="25%">
                        <asp:TextBox ID="textBoxLastName" runat="server"  CssClass="textBox" />
                        <br />
                    </td>
                    <td class="textRightAlign" width="25%">
                        <asp:Label ID="labelDOB" runat="server" Text="Date of Birth" />
                    </td>
                    <td class="textLeftAlign" width="25%">
                        <asp:TextBox ID="textBoxDOB" runat="server"  CssClass="textBox" />
                        <cc1:CalendarExtender ID="CalendarExtenderAppDate" runat="server" Enabled="True" Format="MM/dd/yyyy" TargetControlID="textBoxDOB">
                        </cc1:CalendarExtender>
                    </td>
                </tr>
                    <tr>
                    <td class="textCenterAlign" colspan="4">
                        <br />
                        <asp:Button ID="buttonSearch" runat="server" CssClass="buttonLarge" 
                            Text="Search" onclick="buttonSearch_Click" ClientIDMode="Static" CausesValidation="false" EnableViewState="false" />
                        <br />
                        <br />
                    </td>
                </tr>
                    <tr>
                    <td colspan="4">
                        <br />
                        <asp:GridView ID="GridViewPatients1" runat="server" AutoGenerateColumns="False" CssClass="GridViewFullWidthCss"
                             EmptyDataText="No Patients are available" AllowPaging="True"  OnRowCommand="GridViewPatients1_RowCommand"
                            AllowSorting="false" EnableViewState="true" ClientIDMode="Static" >

                            <Columns>

                                  <asp:TemplateField HeaderText="Select">
                                     <ItemTemplate>
                                   <asp:LinkButton ID="linkButtonSelect" runat="server" CommandName="Select" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"PatientID") %>' CausesValidation="false" OnCommand="SelectPatient" Text="Select" EnableViewState="true"></asp:LinkButton>

                                     </ItemTemplate>

                                  </asp:TemplateField>


                                <asp:BoundField DataField="PatientID" HeaderText="Patient ID" SortExpression="PatientID">
                                    <ItemStyle Width="20%" />
                                </asp:BoundField>
                                <asp:BoundField DataField="first_name" HeaderText="First Name" SortExpression="FirstName">
                                    <ItemStyle Width="20%" />
                                </asp:BoundField>
                                <asp:BoundField DataField="last_name" HeaderText="Last Name" SortExpression="LastName">
                                    <ItemStyle Width="20%" />
                                </asp:BoundField>
                                <asp:BoundField DataField="birth_date" HeaderText="Date of Birth" SortExpression="DOB">
                                    <ItemStyle Width="20%" />
                                </asp:BoundField>                                  
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
                </table>
                </td>
                </tr>  
            </table>
        </ContentTemplate>
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="buttonSearch" EventName="click" />          
        </Triggers>
    </asp:UpdatePanel>

And my code behind is like this

protected void SelectPatient(object sender, EventArgs e)
{
        LinkButton lnkSelect = (LinkButton)sender;
        Session["Selected_Patient_Id"] = lnkSelect.CommandArgument;
}
2

There are 2 best solutions below

0
On

Generate RowCommand Event of GridView and use it like ..

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
   if(e.CommandName == "Select")
   {
       //write what you want to do

   }

}
0
On

LinkButton click event doesn't fire because it is replace it's event on page load while you click on it. You should use IsPostBack on Page_Load to avoid this. I think you are every time DataBind to GridView on page load. So Ignore this and use code this way. It will be helpful for your problems.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          grid.DataSource = dsGrid;
          grid.DataBind();
        }
    }