简体   繁体   English

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

[英]Is it possible to programmatically input values in TextBox control before UpdateCommand runs

(C# asp.net 3.5) I have successfully pre-populated CheckBoxList3 in RowDataBound event. (C#asp.net 3.5)我已经在RowDataBound事件中成功预填充了CheckBoxList3 In edit mode, user may then make other checkbox selections. 在编辑模式下,用户然后可以选择其他checkbox I have successfully captured the new values, creating a new comma-delimited string that updates SQL in _RowUpdating event after Update link is clicked. 我已经成功捕获了新值,并创建了一个新的逗号分隔的字符串,该字符串在单击“更新”链接后更新了_RowUpdating事件中的SQL。 The problem is my update is being overriden by the GridView1s update. 问题是我的更新被GridView1s更新覆盖。 * The new string is not physically input by user in the TextBox2 control. * 新字符串不是由用户实际输入到TextBox2控件中的。

It seems I have two choices: 看来我有两个选择:

  1. Pass the comma-delimited string built from checkboxlist3 selections to TextBox2 control programmatically before UpdateCommand is run. 在运行UpdateCommand之前,以编程方式将根据checkboxlist3选择生成的逗号分隔字符串传递给TextBox2控件。 P* Is this possible? P * 这可能吗? * I've googled everywhere with no clearcut solutions. *我到处搜索Google,没有明确的解决方案。 I've also tried this code in RowUpdating and it makes to difference: 我也在RowUpdating中尝试了这段代码,这有所作为:

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

  2. Update sql manually like I'm doing only place Sql call AFTER the "natural" update (for lack of better words). 手动更新sql,就像我只在“自然”更新(因为缺少更好的单词)之后才放置sql调用一样。 If this is an option, what method to run the update in because placing it in RowUpdating always gets reversed. 如果这是一个选项,则由于将更新放置在RowUpdating中始终会反转执行更新的方法

HTML: HTML:

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

C# C#

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

The solution is to pass new values to Update Command Parameters in SqlDataSource _Updating event. 解决方案是将新值传递给SqlDataSource _Updating事件中的“更新命令参数”。 Relevent updated code provided above. 释放上面提供的更新代码。

I removed the SQL update routine that I had in _RowUpdating event entirely - it isn't needed. 我完全删除了_RowUpdating事件中具有的SQL更新例程-不需要。 Then I saved the newly created comma-delimited string to ViewState object which I retrieve in SqlDataSource _Updating event. 然后,我将新创建的逗号分隔的字符串保存到ViewState对象,该对象在SqlDataSource _Updating事件中检索。

Credit for me coming to this conclusion goes to leoinlios because of his post here: changing textbox value in code behind does not post back new value Plus I did a lot of reading about View State and found this to be the most useful article: TRULY Understanding ViewState 对我来说,归功于leoinlios是因为leoinlios在这里发表了他的文章: 在后面的代码中更改文本框的值不会回传新的值,此外,我阅读了很多有关View State的文章,发现这是最有用的文章: TRULY理解ViewState

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 保留我在C#中以编程方式创建的TextBox控件的值 - Retaining values of TextBox control I've programmatically created in C# C# 是否可以在不覆盖文本框的第一个输入值的情况下在一个文本框中获取两个输入文本框值? - C# Is it possible to get the two input textbox values in one textbox without overwriting the first input value of textbox? 不抛出OptimisticConcurrencyException:可能的原因:UpdateCommand - OptimisticConcurrencyException is not thrown: Possible reason: UpdateCommand 是否可以流式传输到TextBox或类似的控件对象? - Is it possible to stream into a TextBox or similar control object? 以编程方式为文本框输入创建按键绑定 - Programmatically create a key binding for textbox input 尝试自动填充值以输入文本框 - Trying to Autofill values to input textbox 是否可以在提交之前从文本框中检索值? - Is it possible to retrieve the value from textbox before submission? 文本框输入控件(字符数,字符类型) - Textbox input control (number of characters, type of characters) 用于控制范围验证器或文本框最大输入双精度的复选框 - Checkbox to control rangevalidator or maximum input double of textbox 是否可以将一个文本框控件的值绑定到另一个文本块控件? - Is it possible to bind the value of one textbox control to another textblock control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM