Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在UpdateCommand运行之前,是否可以通过编程方式在TextBox控件中输入值_C#_Asp.net_Sql_Gridview_Textbox - Fatal编程技术网

C# 在UpdateCommand运行之前,是否可以通过编程方式在TextBox控件中输入值

C# 在UpdateCommand运行之前,是否可以通过编程方式在TextBox控件中输入值,c#,asp.net,sql,gridview,textbox,C#,Asp.net,Sql,Gridview,Textbox,(C#asp.net 3.5)我已成功地在RowDataBound事件中预填充了CheckBoxList3。在编辑模式下,用户可以选择其他复选框。我已经成功地捕获了新值,创建了一个新的逗号分隔字符串,在单击更新链接后在\u rowUpdate事件中更新SQL。问题是我的更新被GridView1s更新覆盖*新字符串不是用户在TextBox2控件中实际输入的 看来我有两个选择: 传递从checkboxlist3选择生成的逗号分隔字符串 在运行UpdateCommand之前以编程方式访问TextBox

(C#asp.net 3.5)我已成功地在
RowDataBound
事件中预填充了
CheckBoxList3
。在编辑模式下,用户可以选择其他
复选框。我已经成功地捕获了新值,创建了一个新的逗号分隔字符串,在单击更新链接后在
\u rowUpdate
事件中更新SQL。问题是我的更新被GridView1s更新覆盖*新字符串不是用户在TextBox2控件中实际输入的

看来我有两个选择:

  • 传递从checkboxlist3选择生成的逗号分隔字符串 在运行UpdateCommand之前以编程方式访问TextBox2控件。这可能吗?*我已经 谷歌到处搜索,没有明确的解决方案。我也在行更新中尝试了这段代码,它起到了不同的作用:

    textbbox tb2=(TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl(“TextBox2”);
    tb2.Text=strCheckBoxList3.Substring(0,strCheckBoxList3.Length-2)

  • 手动更新sql,就像我只在“自然”更新之后调用sql一样(因为缺少 更好的词语)。如果这是一个选项,中运行更新的方法是什么,因为将其放置在行更新中总是会被反转

  • HTML:


    解决方案是在SqlDataSource\u更新事件中传递新值以更新命令参数。上面提供的相关更新代码

    我完全删除了我在更新事件中使用的SQL更新例程——它不是必需的。然后,我将新创建的逗号分隔字符串保存到ViewState对象中,并在SqlDataSource\u更新事件中检索该对象


    leoinlios在这里发表了一篇文章,这是我得出这个结论的功劳:另外,我读了很多关于视图状态的文章,发现这是最有用的一篇文章:

    你在处理RowUpdate事件吗?在RowUpdated事件中,您是否可以检查
    e.Exception
    ,以查看是否在更新过程中引发了一些异常?我想我没有清楚地解释我的问题。进入RowUpdate事件(e)的第二个参数具有“Exception”属性。是
    e.Exception
    null,还是其中包含某些内容?e.Exception在行中为null
    <asp:TemplateField HeaderText="Endorsements" SortExpression="Endorsements">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Endorsements") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Endorsements") %>'></asp:TextBox>
                    <asp:CheckBoxList ID="CheckBoxList3" runat="server" Font-Size="Small" RepeatDirection="Horizontal" onselectedindexchanged="CheckBoxList3_SelectedIndexChanged" 
                        AutoPostBack="True" >
                        <asp:ListItem Value="H">H</asp:ListItem>
                        <asp:ListItem Value="I">I</asp:ListItem>
                        <asp:ListItem Value="K">K</asp:ListItem>
                        <asp:ListItem Value="N">N</asp:ListItem>
                        <asp:ListItem Value="T">T</asp:ListItem>
                        <asp:ListItem Value="X">X</asp:ListItem>
                    </asp:CheckBoxList>
                </EditItemTemplate>
            </asp:TemplateField>
    
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {             
                //endorsements string
                string strCheckBoxList3 = String.Empty;
    
                //find endorsements checkboxlist in gridview. 
                CheckBoxList cbl3 = (CheckBoxList)GridView1.Rows[e.RowIndex].Cells[1].FindControl("CheckBoxList3");
    
                try
                {
                    // Build Endorsements string
                    if (cbl3 != null)
                    {
                        // determine which checkboxes have been checked 
                        foreach (ListItem item in cbl3.Items)
                        {
                            // is item checked?
                            if (item.Selected == true)
                            {
                                // build string  
                                strCheckBoxList3 += (item.Value + ", ");
    
                            }//end of if
                        }// end of foreach
    
                        // Save the value in ViewState object before the PostBack
                        ViewState["vsEndorsementsString"] = strCheckBoxList3; 
                    }// end of if
    
                }// end of endorsements try
    
                catch (ArgumentOutOfRangeException ez)
                {
                    System.Diagnostics.Debug.WriteLine(ez.Message + "; " + ez.Source + "; " + ez.TargetSite);
                }
    //Note: routine to update SQL was removed  here
    }
    
    
    // New: pass strings to sql Update Command Parameters for two checkboxlist columns in gridview
        protected void sdsMySqlDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
        {
            string getViewStateEndorsementsString = ViewState["vsEndorsementsString"].ToString();
            string getViewStateRestrictionsString = ViewState["vsRestrictionsString"].ToString();
    
            foreach (System.Data.Common.DbParameter p in e.Command.Parameters)
            {
               if (p.ParameterName == "@Endorsements" && p.Value != null) 
               {
                    //Assign @Endorsements parameter
                    e.Command.Parameters["@Endorsements"].Value = getViewStateEndorsementsString.ToString();
                }//if
    
                if (p.ParameterName == "@Restrictions" && p.Value != null) 
                {
                    //Assign @Restrictions parameter
                    e.Command.Parameters["@Restrictions"].Value = getViewStateRestrictionsString.ToString();
                }//if
            } 
        }