2017-10-18 20 views
0

我有下面的代码的问题,标记如下:如何在asp.net传递文本框的值的代码更新上点击

<asp:GridView ID="GridView" runat="server" 
      AutoGenerateEditButton="True" 
      OnRowDataBound="GridView_RowDataBound" 
      OnRowEditing="GridView_RowEditing" 
      OnRowUpdating="GridView_RowUpdating" 
      CssClass="gridv"> 

      <Columns> 

       <asp:TemplateField HeaderText="ASN"> 
        <ItemTemplate> 
         <asp:Label ID="lblASN" runat="server" Text='<% #Eval("ASN")%>'></asp:Label> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtASN" runat="server" Text='<%# Bind("ASN")%>' CssClass="form-control"></asp:TextBox> 
        </EditItemTemplate> 
       </asp:TemplateField> 
... 

然而,当我运行此代码以获得新从文本框改变的值已成功生成和填充的,我只得到初始值不是消息,用户已经进入,这背后的代码是:

Protected Sub GridView_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView.RowUpdating 
    Try 
     Dim row As GridViewRow = GridView.Rows(e.RowIndex) 
     Dim ID As Integer = DirectCast(row.FindControl("txtID"), TextBox).Text 

     Dim sASN As String = DirectCast(row.FindControl("txtASN"), TextBox).Text 
     Dim sDescription As String = DirectCast(row.FindControl("txtDescription"), TextBox).Text 
     Dim sManufacturer As String = DirectCast(row.FindControl("ddlmanufacturer"), DropDownList).SelectedValue 

     GridView.EditIndex = -1 
    Catch 
    End Try 
    ShowEmpDetails() 
End Sub 

所以,当我点击我使用的消息更新按钮框中写出上面的变量,我得到的值与最初得到的值相同写入文本框,而不是用户已更改的文本? 我已经从类似的例子中找出了这个代码,其中没有问题,我真的无法弄清楚我做错了什么?

按照要求Page_Load事件中调用此函数:

Private Sub ShowEmpDetails() 
    Dim query As String = "SELECT * from inventory.all_items" 
    Dim cmd As MySqlCommand 
    cmd = New MySqlCommand(query) 
    cmd.Connection = myConn 
    Dim sda As New MySqlDataAdapter 
    sda.SelectCommand = cmd 
    Dim dt As New DataTable 
    sda.Fill(dt) 
    GridView.DataSource = dt 
    GridView.DataBind() 
End Sub 
+0

您能否分享您的Page_Load事件处理程序实现? –

+0

网格是否有偶然的'AutoPostback'属性?你在使用任何'Ajax'控件吗? –

+0

@David Espino,没有一个是使用。 – Dominik

回答

1

好吧......有这么多东西不对您的编码方法,我不知道从哪里开始。抱歉。

让我们再次开始,我将解释如何在DOM和代码隐藏之间正确传递值。

首先,您需要了解DOM如何为浏览器填充和构建HTML以了解正在发生的事情。

我会在Firefox中测试您的项目并使用Inspector工具(右键单击wep页面)。这个工具是黄金,并保存了我已经秃头揭露我的头骨! :-)

如您所知,GridView控件将控件的“视图”和“编辑”部分绑定到相同的代码中。我可以看到你有​​作为控件的视图部分(或者我应该说的控件的模式),编辑模式下有Bind()。那很好。我个人讨厌BoundControls,,因为你无法真正看到底下发生了什么。

接下来,避免使用AutoPostBack像匾!这只是丑陋的。

熟悉AjaxControlToolKit(还有其他人,但以Ajax开头)和ASP:UpdatePanel

你的情况是这样的

所以......

<asp:UpdatePanel ID="upADDMAIN" runat="server" UpdateMode="Conditional"> 
    <ContentTemplate> 
     <asp:GridView ID="GridView" runat="server" 
     AutoGenerateEditButton="True" 
     OnRowDataBound="GridView_RowDataBound" 
     OnRowEditing="GridView_RowEditing" 
     OnRowUpdating="GridView_RowUpdating" 
     CssClass="gridv"> 

尝试,把尽可能多的功能回到GridView控制的默认设置。因此,请回到VStudio中的DESIGNER模式,并在GridView.的设计模式中添加所需的所有功能,例如编辑,更新,删除等。这还将确保您的SQLDataSource在同一时间通过正确的SQL更新任务。

现在你为什么要使用OnRowEditingOnRowUpdating

我的经验法则总是把事情控制在最低限度,尽可能多地控制ASP.net。这避免了重新发明具有ASP.net可以直接处理的代码隐藏内容的轮子。

我通常使用OnDataBound(), OnRowDataBound(), and OnRowUpdating()来读取数据并在Update()被控件调用之前预先更新数据。

即:

protected void gvLogins_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    GridViewRow gvRow = (GridViewRow)e.Row; 
    { 
     if (gvRow.RowType == DataControlRowType.DataRow) 
     { 

protected void gvLogins_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    //apply values to the SQL parameters for UPDATE, etc 
    GridViewRow gvRow = (GridViewRow)gvLogins.Rows[e.RowIndex]; 

做的GridView的以外的一些更新前的更新通知。

我从不在PageLoad()中预渲染或预加载数据。这只是重新发明轮子,默认情况下,大多数ASP.net控件已经具有内置的连接和更新!

哦,并获得GridView控件内的值...只需使用FindControl(),但在正确的地方!即:DataBound()事件等。

  DropDownList ddlAgent = (DropDownList)gvRow.FindControl("ddlAgent"); 
      HiddenField hfAgentID = (HiddenField)gvRow.FindControl("hfAgentID"); //from overall state,as EDIT mode defaults the hfAgentID to 0! 
      if (ddlAgent != null && hfAgentID != null) 
       ddlAgent.SelectedValue = hfAgentID.Value; 

对不起,我只使用C#,而不是VB。

祝你好运。

+1

感谢这让我走上了正轨 – Dominik

相关问题