GridView如何从编辑MDOE中的文本框中获取值 [英] GridView How to get the value from a textbox in EDIT MDOE

查看:124
本文介绍了GridView如何从编辑MDOE中的文本框中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有数据绑定项目的gridview。它绑定到SQLDATASOURCE。默认的编辑,更新工作正常,但是,我想在用户更新行时也执行查询。

这是我的aspx

 < asp:GridView ID =GridView1runat =serverAutoGenerateColumns =FalseDataKeyNames =Customer_id
DataSourceID =SqlDataSource1
EmptyDataText =There没有要显示的数据记录。 AllowPaging =True
AllowSorting =TrueCellPadding =4ForeColor =#333333GridLines =Horizo​​ntal
PageSize =5Width =873pxOnRowCommand =RunCustomMethod> ;
< AlternatingRowStyle BackColor =WhiteForeColor =#284775/>
<列>
< asp:TemplateField>
< EditItemTemplate>
CommandName =myCustomUpdateMethodText =UpdateCommandArgument ='<%#Eval(Customer_ID )%>'
onclientclick =return确认('您确定要进行这些更改?')>< / asp:LinkBut​​ton>
& nbsp;< asp:LinkBut​​ton ID =LinkBut​​ton2runat =serverCausesValidation =False
CommandName =CancelText =Cancel>< / asp:LinkBut​​ton> ;
< / EditItemTemplate>
< ItemTemplate>
< asp:LinkBut​​ton ID =LinkBut​​ton1runat =serverCausesValidation =False
CommandName =EditText =Edit>< / asp:LinkBut​​ton>
& nbsp;
< asp:LinkBut​​ton ID =LinkBut​​ton2runat =serverCausesValidation =False
CommandName =SelectText =Select>< / asp:LinkBut​​ton>
& nbsp;
< / ItemTemplate>
< / asp:TemplateField>
< asp:BoundField DataField =Customer_idHeaderText =Customer_idReadOnly =True
SortExpression =Customer_idInsertVisible =False/>
< asp:TemplateField HeaderText =Customer_NameSortExpression =Customer_Name>
< EditItemTemplate>
< asp:TextBox ID =TextBox1runat =serverText ='<%#Bind(Customer_Name)%>>< / asp:TextBox>
< / EditItemTemplate>
< ItemTemplate>
< asp:Label ID =Label1runat =serverText ='<%#Bind(Customer_Name)%>>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>

< /列>

< / asp:GridView>

这里是我的c#代码



<$ p (e.CommandName ==myCustomUpdateMethod)
{
//自定义方法更新行
保护无效RunCustomMethod(对象发件人,GridViewCommandEventArgs e)
{
int customerID = Convert.ToInt32(e.CommandArgument);

SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString);
SqlCommand Cmd = new SqlCommand();

尝试
{
conn.Open();

Cmd.CommandText =更新客户设置customer_name ='+ **我想获得更新的价值,但我从哪里得到它????? ** +',modified_on = getdate(),modified_by ='+ System.Environment.UserName +'where customer_id ='+ customerID +';
Cmd.CommandType = System.Data.CommandType.Text;
Cmd.Connection = conn;

Cmd.ExecuteNonQuery();
GridView1.DataBind();

//关闭编辑模式
GridView1.EditIndex = -1;

$ b catch(SqlException ee)
{

ValidationError.Display(ee.Message);

}

finally
{
Cmd.Dispose();
conn.Close();
conn.Dispose();





$ b $ / code>

任何帮助将不胜感激

解决方案

我认为你正在寻找的代码因为是:

  GridViewRow row =(GridViewRow)(((LinkBut​​ton)e.CommandSource).NamingContainer); 
string name =((TextBox)row.FindControl(TextBox1))。Text
$ b Cmd.CommandText =update Customers set customer_name ='+ name +',modified_on = getdate(),modified_by ='+ System.Environment.UserName +'where customer_id ='+ customerID +';


I have a gridview that has databound items. it is bound to a SQLDATASOURCE. The default Edit, Update works fine, however, i wanted to perform a query too when user updates the row.
Here is my aspx

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Customer_id"
        DataSourceID="SqlDataSource1" 
        EmptyDataText="There are no data records to display." AllowPaging="True" 
        AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="Horizontal" 
        PageSize="5" Width="873px"  OnRowCommand = "RunCustomMethod" >
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:TemplateField>
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
                        CommandName="myCustomUpdateMethod" Text="Update" CommandArgument = '<%# Eval("Customer_ID") %>'
                        onclientclick="return Confirm ('Are You Sure You Want To Make These Changes?')"></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                        CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                        CommandName="Edit" Text="Edit" ></asp:LinkButton>
                    &nbsp;
                    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                        CommandName="Select" Text="Select"></asp:LinkButton>
                    &nbsp;
                 </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Customer_id" HeaderText="Customer_id" ReadOnly="True"
                SortExpression="Customer_id" InsertVisible="False" />
            <asp:TemplateField HeaderText="Customer_Name" SortExpression="Customer_Name">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Customer_Name") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Customer_Name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>

    </asp:GridView>

And here is my c# Code

  protected void RunCustomMethod(object sender, GridViewCommandEventArgs e)
        {
                // Custom Method To Update Row
            if (e.CommandName == "myCustomUpdateMethod")
            {
                int customerID = Convert.ToInt32(e.CommandArgument);

                   SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString);
                SqlCommand Cmd = new SqlCommand();

                try
                {
                    conn.Open();

                    Cmd.CommandText = "update Customers set customer_name = '" + **I WANT TO GET THE UPDATED VALUE BUT FROM WHERE SHALL I GET IT?????** + "', modified_on = getdate(), modified_by = '" + System.Environment.UserName + "' where customer_id = '" + customerID + "'";
                    Cmd.CommandType = System.Data.CommandType.Text;
                    Cmd.Connection = conn;

                    Cmd.ExecuteNonQuery();
                    GridView1.DataBind();

                    // Close the Edit Mode
                    GridView1.EditIndex = -1;

                }
                catch (SqlException ee)
                {

                    ValidationError.Display(ee.Message);

                }

                finally
                {
                    Cmd.Dispose();
                    conn.Close();
                    conn.Dispose();

                }
            }

        }

    }

Any help would be greatly appreciated

解决方案

I think the code you're looking for is:

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
string name = ((TextBox)row.FindControl("TextBox1")).Text

Cmd.CommandText = "update Customers set customer_name = '" + name + "', modified_on = getdate(), modified_by = '" + System.Environment.UserName + "' where customer_id = '" + customerID + "'";

这篇关于GridView如何从编辑MDOE中的文本框中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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