在ASP.NET C#中单击按钮时无法获取网格视图内的文本框的值

bvuwiixz  于 5个月前  发布在  .NET
关注(0)|答案(2)|浏览(68)

我有一个网格视图,我想在网格视图内的文本框中写一些评论。

protected void btnRejectSite_Click(object sender, EventArgs e)
{
    string strRemarks = string.Empty; string strID = string.Empty; string strCurrentUser = string.Empty;

    foreach (GridViewRow row in rptHotoIPDataInfo.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
            strID = (row.Cells[1].FindControl("lbl_ID") as Label).Text;

            TextBox txtRemarks = (TextBox)row.FindControl("txtRemarks");
            strRemarks = txtRemarks.Text; // here I want the text

            strCurrentUser = CurrentUserName;
            int intGroupID = CurrentUserGroupID;
        }
    }
}

字符串
但是当我调试txtRemarks.Text时,我总是得到null
这是gridview aspx标记:

<asp:GridView ID="rptHotoIPDataInfo" runat="server"  
     AutoGenerateColumns="false" CssClass="appRejTableOuter appRejTable" Visible="true"
     AllowPaging="true" PageSize="20" 
     OnPageIndexChanging="rptHotoIPDataInfo_PageIndexChanging"
     DataKeyNames="SAP_ID" 
     OnRowEditing="rptHotoIPDataInfo_RowEditing" 
     OnRowUpdating="rptHotoIPDataInfo_RowUpdating"
     OnRowCancelingEdit="rptHotoIPDataInfo_RowCancelingEdit" 
     OnRowDataBound="rptHotoIPDataInfo_RowDataBound"
     OnRowCommand="rptHotoIPDataInfo_RowCommand" 
     EnableViewState="true">

    <Columns>
        <asp:TemplateField HeaderStyle-CssClass="appRejTable">
            <ItemTemplate>
                <asp:CheckBox ID="chkRow" runat="server" Checked="false" AutoPostBack="true" CssClass="appRejCheckbox" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ID" ItemStyle-Width="50px" Visible="false">
            <ItemTemplate>
                <asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Rejected Remarks" HeaderStyle-CssClass="appRejTable">
            <ItemTemplate>
                <asp:TextBox ID="txtRemarks" Text='<%# Eval("REJECT_REMARKS") %>' runat="server" CssClass="appRejInput"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="SAP_ID" HeaderText="SAP ID"></asp:BoundField>
        <asp:BoundField DataField="CHANGEREQUEST_ID" HeaderText="Change Request Number"></asp:BoundField>
        <asp:BoundField DataField="R4GSTATE_CODE" HeaderText="State Code"></asp:BoundField>
        <asp:BoundField DataField="HOTO_STATUS" HeaderText="Hoto Status"></asp:BoundField>
        <asp:BoundField DataField="CR_Justifications" HeaderText="CR Justifications"></asp:BoundField>
        <asp:BoundField DataField="APPROVE_REJECT" HeaderText="Status"></asp:BoundField>
        <asp:BoundField DataField="REJECTED_BY" HeaderText="Last Updated by"></asp:BoundField>
    </Columns>
</asp:GridView>


这是怎么了?

uidvcgyl

uidvcgyl1#

下面是修改后的代码版本,它使用层次结构在GridView中查找TextBox

protected void btnRejectSite_Click(object sender, EventArgs e)
{
    string strRemarks = string.Empty; 
    string strID = string.Empty; 
    string strCurrentUser = string.Empty;

    foreach (GridViewRow row in rptHotoIPDataInfo.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
            strID = (row.Cells[1].FindControl("lbl_ID") as Label).Text;

            TextBox txtRemarks = (TextBox)row.FindControl("txtRemarks");
            if (txtRemarks != null)
            {
                strRemarks = txtRemarks.Text;
                // Your other logic here
            }

            strCurrentUser = CurrentUserName;
            int intGroupID = CurrentUserGroupID;
        }
    }
}

字符串
确保btnRejectSite_Click方法正确连接到标记或代码隐藏中的按钮的click事件。如果问题仍然存在,您可能需要检查任何可能干扰正常回发行为的客户端JavaScript。
如果remarks为null,则显示警报,可以使用JavaScript显示客户端警报。可以使用ScriptManager注册启动脚本。

if (string.IsNullOrEmpty(strRemarks))
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Remarks cannot be empty.');", true);
    return; // or handle accordingly
}

icomxhvb

icomxhvb2#

好了,这里的规则是:
绑定/内置在列中,您可以使用cells()集合。
模板字段时,不使用单元格()集合,而使用Row.FindControl(“控件名称”)。
因此,这应该起作用:

CheckBox chkRow = (CheckBox)row.FindControl("chkRow");
        Label lblID = (Label)row.FindControl("lbl_ID");
        TextBox txtRemarks = (TextBox)row.FindControl("txtRemarks");

字符串
因此,现在您可以使用chkRow.xml,并使用标签和文本框的.text属性。

相关问题