代码之家  ›  专栏  ›  技术社区  ›  Baharanji

DataGrid中EditTemplate内的文本框返回空文本

  •  0
  • Baharanji  · 技术社区  · 15 年前

    我想捕获用户在按下更新命令时输入的文本,考虑到我将编辑和更新命令更改为“激活”。

     <asp:DataGrid ID="Datagrid1" runat="server" AutoGenerateColumns="False" Width="100%"
        OnCancelCommand="Datagrid1_CancelCommand" OnEditCommand="Datagrid1_EditCommand"
        OnUpdateCommand="Datagrid1_UpdateCommand">
        <Columns>
            <asp:TemplateColumn>
                  <EditItemTemplate>
                    <table cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td align="center" style="width: 200px; white-space: nowrap;">
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Reg_FullName") %>'></asp:Label>
                            </td>
                            <td align="center" style="width: 100px;">
                                <asp:Label ID="lbl_User" runat="server" Text='<%# Eval("Reg_UserName") %>'></asp:Label>
                            </td>
                            <td align="center" style="width: 100px;">
                                <asp:Label ID="lbl_AddedAt" runat="server" Text='<%# Eval("Reg_ActivatedAt") %>'></asp:Label>
                            </td>
                            <td align="center" style="width: 100px;">
                                <asp:Label ID="lbl_IsAct" runat="server" Text='<%# Eval("Reg_IsActive") %>'></asp:Label>
                            </td>
                            <td align="center" style="width: 100px;">
                                <asp:Label ID="lbl_Days" runat="server" Text="<%$ Resources:Okaz, DaysNum %>"></asp:Label>
                            </td>
                            <td align="center" style="width: 100px;">
                                <asp:TextBox ID="Hello_txt" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </EditItemTemplate></asp:TemplateColumn>
            <asp:EditCommandColumn CancelText="Cancel" EditText="Activate"
                UpdateText="Activate"></asp:EditCommandColumn>
    

    我的代码是:

    protected void Datagrid1_EditCommand(object source, DataGridCommandEventArgs e)
        {
            Datagrid1.EditItemIndex = e.Item.ItemIndex;
            try
            {
                SqlDataSource1.SelectCommand = "Select * from OkazRegisteration";
                Datagrid1.DataSourceID = SqlDataSource1.ID;
                Datagrid1.DataBind();
            }
            catch (Exception ex)
            {
                ErrorLabel.Text = ex.ToString();
            }
        }
    
        protected void Datagrid1_CancelCommand(object source, DataGridCommandEventArgs e)
        {
            Datagrid1.EditItemIndex = -1;
            try
            {
                SqlDataSource1.SelectCommand = "Select * from OkazRegisteration";
                Datagrid1.DataSourceID = SqlDataSource1.ID;
                Datagrid1.DataBind();
            }
            catch (Exception ex)
            {
                ErrorLabel.Text = ex.ToString();
            }
        }
    
        protected void Datagrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
        {
    
            switch (e.Item.ItemType)
            {
                case ListItemType.EditItem:
    
                    TextBox txt_Days = (TextBox)e.Item.FindControl("Hello_txt");
                    Label lbl_User = (Label)e.Item.FindControl("lbl_User");
    
                    SqlDataSource2.UpdateCommand = "Update OkazRegisteration set Reg_IsActive='True', Reg_ExpiryDays=" + txt_Days.Text + " , Reg_ActivatedAt='" + DateTime.Now.ToShortDateString() + "' Where Reg_UserName='" + lbl_User.Text + "'";
                    SqlDataSource2.Update();
                    break;
            }
            try
            {
                SqlDataSource1.SelectCommand = "Select * from OkazRegisteration";
                Datagrid1.DataSourceID = SqlDataSource1.ID;
                Datagrid1.DataBind();
            }
            catch (Exception ex)
            {
                ErrorLabel.Text = ex.ToString();
            }
        }
    

    在数据报中1_updateCommand

    文本框txt_天的文本值始终为空

    我做错什么了??

    我做这种工作已经很长时间了,但这次我看不到错误。

    请帮忙,

    当做。

    1 回复  |  直到 14 年前
        1
  •  0
  •   Baharanji    15 年前

    我发现每次加载页面时都绑定网格,这样文本框中的文本就会被删除。

    希望能给任何面临同样问题的人一个暗示

    推荐文章