简体   繁体   中英

Double pass required for TextBox Postback in FormView

This is a follow up to an earlier issue which I provided my own answer to but there is a new wrinkle in the process that I'm looking for an answer.

Scenario:
Have a asp.TextBox embedded inside of a asp.FormView control. Prior issue was that I couldn't populate the TextBox from code behind (C#). This was corrected by adding AutoPostBack = "true" to the TextBox.

Issue:
The postback corrected the population of the Text box but now two passes on the control assignment have to be executed in order to see the textbox populated. 在此处输入图片说明

Specific Question
How can correct this so it doesn't require a second click of the button to see the textbox populated.

Code Examples:

/*-- 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" />&nbsp;<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();

Any insight here would be welcomed. I've tried several things but haven't come up with a means of making this work like I want it to. I'm thinking if I could trigger a second postback of only the TextBox control here that could resolve it. The problem is every method of doing a postback from code behind seems to reset the page which by default has this FormView invisible.

Regards,

Updated 03/24/17 1:06PM Central US: I took a bit of a break from this issue to work on another segment of the project. I tried several more thing but the last thing I did was track several variables through the debugger and examine the flow of the code as I stepped through the pertinent sections of code.

To the double tap issue I discovered that the first time you click on the Insert Implementation Standards button that the execution was routed through 'Item Template' of the FormView and then it ran through the 'Insert Template' before coming to rest.

Conversely the second click of the button executed as I would have expected and ONLY executed the 'Insert Template of the FormView.

I also observed all the variables that are used in the DataSource related to the FormView being properly populated prior to the execuation of either the Item Template and/or the Insert Template of the FormView.

So, this is even more baffling that I originally ran into it because at first all that I was dealing with was the lack of visibility of two TextBoxes, now however, we have the curiosity of the code executing the Item Template of the FormView prior to walking through the Insert Template.
Ken...

Updated: 03/27/17 9:55AM Central US I have been working on this and searching for examples of similar issues that I might be able to leverage from and so have now attempted using a FormView.ItemInserting block (Which is bypassed prior to actual execution of the 'insert') and FormView.DataBound blocks. The DataBound was able to trigger but I had to encompass the block in a conditional 'if (IsPostBack)' to avoid trigging a null reference error. This properly routed through the block however the result on the screen draw was the same wit the fields not being populated as intended. Here is the last block as last attempted:

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();
        }
}

Pressing on but could really use some other eyes on this.

Update: 03/29/17 7:42AM Central US

I wanted to post this update yesterday but got pulled off on a different project for a bit. My latest attempt to resolve this issue I thought for certain that it would work. But it has also failed to load the values into the insert mode of the forum until the button has been selected the second time.

What this does is from the trigger of 'DataBound' in the FormView it directes the flow to a function FillDefaultValue which assigns default values to the two parameters that are in the FormView that control the TextBox displays.

In my mind this 'should have' resolved the issue as the default value would then be the values contained in the Session variables instead of being blank(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();
    }
}

So the mystery continues...

I ended up using the function FillDefaultValueInFormView() defined in the question to resolve this.

I watch in debut the flow over and over again and realized what was happening was that when I set the FormView's mode to Insert that it was flushing the value assignments that I was placing on the TextBox.

Then I realized that what I needed to do was assign the default value in the DataSource to be what I desired in the TextBox 'prior' to setting the mode of the FormVeiw to Insert. So, I added the call to the function in the early portion of my Button_click event immediately followed by changing mode of the FormView to 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...
}

After doing this the change to insert mode on the FormView was successfully prepopulated as I had intended for the user. I'll now change them to ReadOnly so the user can't change them, making them programmatically driven fields within the form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM