Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Page Load only works the first time

Status
Not open for further replies.

kamfl610

Programmer
Apr 15, 2003
90
US
Hi all,

I have a problem with some code and I'm not sure what's going on. When my page first loads, my Page Load event fires fine. I'm locating the footer field of active date and rendering it readonly and setting the onclick on the input button to javascript to fire if clicked. Works great till I click on the edit button on any of my gridview rows and then none of my Page Load event changes get loaded or at least get overwritten by another event. I need a clue here. Posting snippet code for aspx page and then the cs page.

Code:
<asp:TemplateField HeaderText="Active Date" SortExpression="active_date">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("active_date", "{0:MM/dd/yyyy}") %>'
MaxLength="10" Width="70px" ></asp:TextBox><input type="button" runat="server" value="Cal" id="btnCal"/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("active_date", "{0:MM/dd/yyyy}") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtActivedate" runat="server" Text='01/01/2007' MaxLength="10" Width="70px"></asp:TextBox><input
type="button" runat="server" value="Cal" id="btCal" />
</FooterTemplate>
</asp:TemplateField>

cs code snippet:
Code:
protected void Page_Load(object sender, EventArgs e)
{
        DateTime dt = DateTime.Now;
        string str = dt.ToString("MM/dd/yyyy");
        ((TextBox)GridView1.FooterRow.FindControl("txtActivedate")).Attributes.Add("readonly", "true");
        TextBox active_date = GridView1.FooterRow.FindControl("txtActivedate") as TextBox;
        string adate = active_date.ClientID;
        ((TextBox)GridView1.FooterRow.FindControl("txtActivedate")).Text = str;
        ((HtmlInputButton)GridView1.FooterRow.FindControl("btCal")).Attributes.Add("onclick", "displayCalendar(document.getElementById('" + adate + "'),'mm/dd/yyyy',this);");

    }

Also, two sided problem. I can't get to the EditTemplate field to do the same thing. I tried OnRowEditing but that's no good because I can't get to the ClientID. Any ideas?

Thanks for the help.
 
(you should be posting in the forum855 : ASP.NET)

For the first, there is the page load and the postback. Whenever a user types in his browser the the load event is fired. If he refreshed the page, this is a postback.
Check: if (page.ispostback) {}
 
I will post it in that forum. The check for postback didn't affect it either.
 
Just thought I'd let everyone know I found a solution in case anyone wanted to know. I took all my code out of Page_Load because that's not where it gets saved. I found out that there are many steps in the page rendering before it shows up on the web page. After much research and headache, I found you can override each step so I setup Response.Write statements to see what state the variable was at. What I discovered is that in the SaveState, my page was defaulting to what HTML was on the page because it doesn't reload the page or do a postback. So I ended up putting the code in the OnSaveStateComplete. Code below:

Code:
    protected override void OnSaveStateComplete(EventArgs e)
    {
        int index = GridView1.EditIndex;

        if (index >= 0)
        {
            GridViewRow row = GridView1.Rows[index];

            string adate2 = row.FindControl("TextBox2").ClientID;
            ((TextBox)row.FindControl("TextBox2")).Attributes.Add("readonly", "true");
            ((HtmlInputButton)row.FindControl("btnCal")).Attributes.Add("onclick", "displayCalendar(document.getElementById('" + adate2 + "'),'mm/dd/yyyy',this);");
        }
        DateTime dt = DateTime.Now;
        string str = dt.ToString("MM/dd/yyyy");
        ((TextBox)GridView1.FooterRow.FindControl("txtActivedate")).Attributes.Add("readonly", "true");
        TextBox active_date = GridView1.FooterRow.FindControl("txtActivedate") as TextBox;
        string adate = active_date.ClientID;
        ((TextBox)GridView1.FooterRow.FindControl("txtActivedate")).Text = str;
        ((HtmlInputButton)GridView1.FooterRow.FindControl("btCal")).Attributes.Add("onclick", "displayCalendar(document.getElementById('" + adate + "'),'mm/dd/yyyy',this);");

        base.OnSaveStateComplete(e);
    }

This might help you all in the future. If you don't understand, let me know through e-mail. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top