LinkBut​​ton事件不会在Gridview中调用

时间:2012-10-30 11:22:49

标签: c# asp.net gridview postback asplinkbutton

我在更新面板“PanelSearch”中有包含gridview的表,其display属性设置为none 当我第一次加载页面时,我已将表添加到另一个更新面板(主面板)内的占位符中。我在“PanelSearch”中有一个搜索按钮,当用户点击搜索时,我正在从数据库中检索数据并将其绑定到gridview。

gridview有linkbutton作为templatefield和OnCommand事件。 当我点击链接按钮没有事件被触发但我的页面刷新并且“PanelSearch”内的表格消失。

我不知道为什么。谁能帮助我?

我的标记如下;

<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>

我背后的代码就像这样

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

2 个答案:

答案 0 :(得分:0)

生成GridView的RowCommand事件并将其用作..

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

   }

}

答案 1 :(得分:0)

LinkButton点击事件没有触发,因为它会在您点击它时替换它在页面加载时的事件。 您应该在Page_Load上使用IsPostBack来避免这种情况。我认为您在页面加载时每次DataBindGridView。 所以忽略这一点并以这种方式使用代码。这对你的问题很有帮助。

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