DataGrid中EditTemplate内的文本框返回空文本 [英] Text Box inside EditTemplate in a Datagrid is returns empty text

查看:43
本文介绍了DataGrid中EditTemplate内的文本框返回空文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i want to Capture the the Text Entered by a user when the update Command is Pressed taking into Consideration that i Changed the Edit and Update Command To be named Activate

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

好吧,我后面的代码是:

Well and my code behind is :

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();
        }
    }

在Datagrid1_UpdateCommand

in the Datagrid1_UpdateCommand

文本框txt_Days的文本值始终为空

The Text Value of the TextBox txt_Days is always empty

我做错了什么?

我已经做了很长时间了,但是这次我看不到错误

I've done this kind of work for a really long time , but i cannot see the error this time

请帮助

致谢.

推荐答案

我发现每次页面加载时我都在绑定网格,从而删除了文本框中的文本.

I found out that I am binding my grid every time the page loads so that the text inside my text box is erased.

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

Hope that gives a hint to anyone faces the same issue

这篇关于DataGrid中EditTemplate内的文本框返回空文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆