C# FormView中的文本框回发需要双重传递
这是我提供了我自己答案的一个早期问题的后续问题,但在我寻找答案的过程中出现了一个新的问题 场景:C# FormView中的文本框回发需要双重传递,c#,asp.net,textbox,C#,Asp.net,Textbox,这是我提供了我自己答案的一个早期问题的后续问题,但在我寻找答案的过程中出现了一个新的问题 场景: 将asp.TextBox嵌入asp.FormView控件中。之前的问题是我无法从代码隐藏(C#)填充文本框。通过在文本框中添加AutoPostBack=“true”来纠正这一问题 问题: 回发更正了文本框的填充,但现在必须执行两次控制分配,才能看到文本框填充。 具体问题 如何更正此问题,使其不需要再次单击按钮即可查看已填充的文本框 代码示例: /*-- This is the fragment o
将asp.TextBox嵌入asp.FormView控件中。之前的问题是我无法从代码隐藏(C#)填充文本框。通过在文本框中添加AutoPostBack=“true”来纠正这一问题 问题:
回发更正了文本框的填充,但现在必须执行两次控制分配,才能看到文本框填充。 具体问题
如何更正此问题,使其不需要再次单击按钮即可查看已填充的文本框 代码示例:
/*-- This is the fragment of code in the aspx which is related to the issue: --*/
<asp:FormView runat="server" ID="EditImpStndPreamble" DataSourceID="ImpStndPreambDS" OnItemCommand="EditImpStndPreamble_ItemCommand" DefaultMode="Edit" DataKeyNames="ARS_Index">
<InsertItemTemplate>
<asp:Label runat="server" Font-Size="Small" Font-Bold="true" Width="60" Text="Index #"></asp:Label>
<asp:Label Text='<%# Eval("ARSEL_Index") %>' runat="server" ID="ARSEL_IndexLabel1" Width="74" />
<asp:Label runat="server" Width="60" Font-Size="Small" Font-Bold="true" Text="Control #"></asp:Label>
<asp:TextBox Text='<%# Bind("ARSControlNum") %>' runat="server" ID="ARSControlNumTextBox" Width="74" />
<asp:LinkButton runat="server" Text="Insert" CommandName="Insert" ID="InsertButton" CausesValidation="True" /> <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="InsertCancelButton" CausesValidation="False" />
</InsertItemTemplate>
</asp:FormView>
// This is the fragmentof code in the C# code behind related to the issue
// This is where we update the control number
Label ctrnum = (Label)EditElementsFV.Row.FindControl("ARSControlNumLabel");
if (ctrnum != null)
ctrnum.Text = Session["CurrentControl"].ToString();
继续,但可能真的需要一些其他的眼睛
更新:美国中部时间2017年3月29日上午7:42
我昨天想发布这个更新,但在一个不同的项目上有一段时间被拖延了。我最近试图解决这个问题,我确信它会奏效。但在第二次选择按钮之前,它也无法将这些值加载到论坛的插入模式
这是从FormView中的“DataBound”触发器开始的,它将流定向到函数FillDefaultValue,该函数将默认值分配给FormView中控制文本框显示的两个参数
在我看来,这“应该”解决了这个问题,因为默认值将是会话变量中包含的值,而不是空值(null)
所以谜团还在继续…我最终使用问题中定义的函数FillDefaultValueInFormView()来解决这个问题 我一遍又一遍地观察这个流程,意识到当我将FormView的模式设置为Insert时,它正在刷新我放在文本框上的值赋值 然后我意识到,我需要做的是在设置FormVeiw的插入模式之前,在数据源中将默认值指定为文本框“previor”中所需的值。因此,我在Button_click事件的早期部分添加了对函数的调用,随后将FormView的模式更改为Insert
protected void AddImpStadBTN_Click(object sender, EventArgs e)
{
Session["CurrentColumn"] = "IMP";
FillDefaultValueInFormView();
EditElementsFV.ChangeMode(FormViewMode.Insert);
... More code follows to complete this but left off as it doesn't effect the results...
}
完成此操作后,FormView上插入模式的更改已成功预填充,正如我为用户准备的那样。我现在将它们更改为只读,以便用户无法更改它们,使它们成为表单中的编程驱动字段。您应该在页面加载中更改回发检查,如下所示:
if(IsPostBack)//填充您的文本框else//执行其他操作
@M Adeel Khaild,我修改了页面加载以检查回发,并验证会话变量已经初始化,并且没有任何区别。我在调试中跟踪了它,流跟踪了成功通过if语句的预期位置,但仍然必须双击按钮才能查看文本框的填充。问题更新:美国中部时间3/24/17 1:06PM。问题更新:美国中部时间3/29/17 7:42AM
public void FillDefaultValueInFormView()
{
if (EditElementsFV.CurrentMode == FormViewMode.Insert)
{
EditElementsDS.SelectParameters["ARSControlNum"].DefaultValue = Session["CurrentControl"].ToString();
EditElementsDS.SelectParameters["ColumnTag"].DefaultValue = Session["CurrentColumn"].ToString();
}
}
protected void EditElementsFV_DataBound(object sender, EventArgs e)
{
if (IsPostBack)
{
FillDefaultValueInFormView();
}
}
protected void AddImpStadBTN_Click(object sender, EventArgs e)
{
Session["CurrentColumn"] = "IMP";
FillDefaultValueInFormView();
EditElementsFV.ChangeMode(FormViewMode.Insert);
... More code follows to complete this but left off as it doesn't effect the results...
}