【发布时间】:2017-03-21 16:11:12
【问题描述】:
这是对之前问题的跟进,我提供了自己的答案,但在我正在寻找答案的过程中出现了新的问题。
场景:
在 asp.FormView 控件中嵌入一个 asp.TextBox。先前的问题是我无法从后面的代码(C#)填充文本框。这已通过将 AutoPostBack = "true" 添加到 TextBox 中得到纠正。
问题:
回发更正了文本框的填充,但现在必须执行两次控制分配才能看到填充的文本框。
具体问题
如何更正此问题,以便无需再次单击按钮即可看到填充的文本框。
代码示例:
/*-- 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();
欢迎在这里提出任何见解。我已经尝试了几件事,但还没有想出让这项工作像我想要的那样的方法。我在想我是否可以在此处触发仅 TextBox 控件的第二次回发,以解决它。问题是从后面的代码进行回发的每种方法似乎都会重置默认情况下此 FormView 不可见的页面。
问候,
2017 年 3 月 24 日下午 1:06 更新美国中部:
我从这个问题中稍作休息,开始研究项目的另一部分。我又尝试了几件事,但我做的最后一件事是通过调试器跟踪几个变量,并在我逐步执行相关代码部分时检查代码流。
对于双击问题,我发现第一次单击“插入实施标准”按钮时,执行是通过 FormView 的“项目模板”路由的,然后在停止之前通过“插入模板”运行。
相反,第二次单击按钮按我的预期执行,并且只执行了“插入 FormView 的模板”。
我还观察到,在执行项目模板和/或 FormView 的插入模板之前,DataSource 中使用的与 FormView 相关的所有变量都被正确填充。
所以,这比我最初遇到的更加莫名其妙,因为起初我所处理的只是两个文本框缺乏可见性,但是现在,我们对执行项目模板的代码感到好奇遍历插入模板之前的 FormView。
肯...
更新时间:2017 年 3 月 27 日上午 9:55 美国中部 我一直在研究这个问题并寻找我可能能够利用的类似问题的示例,因此现在尝试使用 FormView.ItemInserting 块(在实际执行“插入”之前被绕过)和 FormView.DataBound块。 DataBound 能够触发,但我必须将块包含在条件“if (IsPostBack)”中以避免触发空引用错误。这通过块正确路由,但是屏幕绘制的结果与未按预期填充的字段相同。这是最后一次尝试的最后一个块:
protected void EditElementsFV_DataBound(object sender, EventArgs e)
{
if (IsPostBack)
{
Label ctrnum = (Label)EditImpStndPreamble.Row.FindControl("ARSControlNumLabel");
if (ctrnum != null)
ctrnum.Text = Session["CurrentControl"].ToString();
Label famlbl = (Label)EditImpStndPreamble.Row.FindControl("ColumnTagLabel");
if (famlbl != null)
famlbl.Text = Session["CurrentFamily"].ToString();
}
}
继续努力,但真的可以用其他人的眼光来看待这个问题。
更新:2017 年 3 月 29 日上午 7:42 美国中部
我昨天想发布此更新,但在另一个项目上被拉了一会儿。我最近尝试解决这个问题,我确信它会起作用。但是在第二次选择按钮之前,它也无法将值加载到论坛的插入模式中。
它的作用是在 FormView 中触发“DataBound”,它将流程引导到一个函数 FillDefaultValue,该函数将默认值分配给 FormView 中控制 TextBox 显示的两个参数。
在我看来,这“应该”解决了这个问题,因为默认值将是 Session 变量中包含的值,而不是空白(null)。
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();
}
}
所以谜团还在继续……
【问题讨论】:
-
您应该在 page_load 中更改您的 postBack 检查,如下所示:
if(IsPostBack) //Populate your textBox else //Do something else -
@M Adeel Khaild,我修改了 page_load 以检查回发并验证会话变量是否已初始化并且没有任何区别。我在调试中对其进行了跟踪,并且流跟踪了成功通过 if 语句的预期位置,但仍然必须双击按钮才能查看文本框的填充情况。
-
问题更新时间:美国中部时间 17 年 3 月 24 日下午 1:06。
-
问题更新时间:美国中部时间 2017 年 3 月 29 日上午 7:42