【问题标题】:LinkButton Event does not get called inside GridviewLinkBut​​ton 事件不会在 Gridview 中被调用
【发布时间】:2012-10-19 17:32:33
【问题描述】:

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

gridview 具有链接按钮作为模板字段和 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;
}

【问题讨论】:

    标签: c# asp.net gridview postback asplinkbutton


    【解决方案1】:

    生成GridView的RowCommand事件并像..一样使用它

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2011-01-12
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多