GridView Update doesn't update on first postback

E

Evan M.

Here's my GridView and my SqlDataSource

<asp:GridView ID="ContactHistoryGrid" runat="server"
AutoGenerateColumns="False" DataSourceID="ContactHistoryDS"
DataKeyNames="JobHistoryID"
OnRowCreated="ContactHistoryGrid_RowCreated"
CssClass="GridViewTable"
GridLines="None" CellSpacing="1" CellPadding="3" AllowSorting="True"
AllowPaging="True">
<EmptyDataTemplate>
<div class="NoResultsPanel">
No items were found for your search.
</div>
</EmptyDataTemplate>
<PagerTemplate>
<div style="float: right;">
<asp:Button ID="NextButton" runat="server" Text="Next"
CommandName="Page" CommandArgument="Next" />
<asp:Button ID="LastButton" runat="server" Text="Last"
CommandName="Page" CommandArgument="Last" />
</div>
<div style="float: left;">
<asp:Button ID="FirstButton" runat="server" Text="First"
CommandName="Page" CommandArgument="First" />
<asp:Button ID="PrevButton" runat="server" Text="Prev"
CommandName="Page" CommandArgument="Prev" />
</div>
Page <asp:DropDownList ID="PageDropDown" runat="server"
AutoPostBack="true" />
of <asp:Literal ID="PageCountLiteral" runat="server" />

</PagerTemplate>
<PagerSettings Position="TopAndBottom" />
<PagerStyle CssClass="tableSmallHeaderCell" />
<Columns>
<asp:BoundField DataField="CustomerName" HeaderText="Company"
ReadOnly="True" SortExpression="CustomerName" >
<ItemStyle CssClass="tableDataCell" />
<HeaderStyle CssClass="tableSmallHeaderCell" />
</asp:BoundField>
<asp:BoundField DataField="Position" HeaderText="Position"
SortExpression="Position" >
<ItemStyle CssClass="tableDataCell" />
<HeaderStyle CssClass="tableSmallHeaderCell" />
</asp:BoundField>
<asp:TemplateField HeaderText="Start Date"
SortExpression="StartDate">
<EditItemTemplate>
<asp:TextBox ID="EditStartDateTextBox" runat="server" Text='<%#
Bind("StartDate", "{0:MMM d, yyyy}") %>' />
<asp:HyperLink ID="StartDateCalendarLink" runat="server"
ImageUrl="~/images/calendarButton.png" />
</EditItemTemplate>
<ItemTemplate>
<asp:Literal ID="StartDateLiteral" runat="server" Text='<%#
Eval("StartDate", "{0:MMM d, yyyy}") %>' />
</ItemTemplate>
<ItemStyle CssClass="tableDataCell" />
<HeaderStyle CssClass="tableSmallHeaderCell" />
</asp:TemplateField>
<asp:TemplateField HeaderText="EndDate" SortExpression="EndDate">
<EditItemTemplate>
<asp:TextBox ID="EditEndDateTextBox" runat="server" Text='<%#
Bind("EndDate", "{0:MMM d, yyyy}") %>' />
<asp:HyperLink ID="EndDateCalendarLink" runat="server"
ImageUrl="~/images/calendarButton.png" />
</EditItemTemplate>
<ItemTemplate>
<asp:Literal ID="EndDateLiteral" runat="server" Text='<%#
Eval("EndDate", "{0:MMM d, yyyy}") %>' />
</ItemTemplate>
<ItemStyle CssClass="tableDataCell" />
<HeaderStyle CssClass="tableSmallHeaderCell" />
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowEditButton="True">
<HeaderStyle CssClass="tableSmallHeaderCell" />
<ItemStyle CssClass="tableDataCell" />
</asp:CommandField>
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="ContactHistoryDS" runat="server"
ConnectionString="<%$ ConnectionStrings:SalesCRM %>" ProviderName="<%
$ ConnectionStrings:SalesCRM.ProviderName %>"
SelectCommand="SELECT ContactID, FirstName, LastName, JobHistoryID,
CustomerNumber, CustomerName, Position, StartDate, EndDate FROM
dbo.ContactJobHistory_V WHERE ContactID = @contactid AND EndDate IS
NOT NULL" SelectCommandType="text"
UpdateCommand="dbo.UpdateJob_usp"
UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="contactid"
QueryStringField="contactid" />
</SelectParameters>
</asp:SqlDataSource>

Here's my OnRowCreatedEvent:

protected void ContactHistoryGrid_RowCreated (object sender,
GridViewRowEventArgs e) {
GridView gv = (GridView) sender;
if (e.Row.RowType == DataControlRowType.Header) {
// Apply the sorting arrows to the header as needed.
GridViewFunctions.AddSortArrow (gv, e);
} else if (e.Row.RowType == DataControlRowType.Pager) {
// Setup the paging buttons (enable/disable the buttons, attach the
proper event function to the dropdownlist)
Button first = (Button) e.Row.Cells[0].FindControl ("FirstButton");
Button previous = (Button) e.Row.Cells[0].FindControl
("PrevButton");
Button next = (Button) e.Row.Cells[0].FindControl ("NextButton");
Button last = (Button) e.Row.Cells[0].FindControl ("LastButton");
DropDownList pageSelect = (DropDownList) e.Row.Cells[0].FindControl
("PageDropDown");
Literal pageCount = (Literal) e.Row.Cells[0].FindControl
("PageCountLiteral");
GridViewFunctions.InitializeButtonPager (gv, e, first, previous,
pageSelect, pageCount, next, last);
} else if (e.Row.RowType == DataControlRowType.DataRow &&
((e.Row.RowState == (DataControlRowState.Alternate |
DataControlRowState.Edit)) || e.Row.RowState ==
DataControlRowState.Edit)) {
// Set the proper javascript function for the image link to open
the popup calendar
string script = "javascript:eek:penCalendar('{0}', '{1}');";
HyperLink calLink = (HyperLink) e.Row.FindControl
("StartDateCalendarLink");
TextBox startDateTB = (TextBox) e.Row.FindControl
("EditStartDateTextBox");
DataRowView dataItem = (DataRowView) e.Row.DataItem;
if (dataItem != null) {
string startDateString = ((DateTime)
dataItem.Row.ItemArray[7]).ToString ("d-MMM-yyyy");
calLink.NavigateUrl = String.Format (script, startDateTB.ClientID,
Server.UrlEncode (startDateString));
}
}
}

The premise here is that in the row that gets edited, I have a textbox
with a link to open a pop-up calendar, that will write back the value
to the text box. That part works. The problem is when I try to click
the update button. The page postsback, but it doesn't leave the "edit"
mode. Instead, it comes back and all the textboxes are now blank. If I
type the values back into the textboxes and submit a second time, my
database gets updated, it leaves edit mode, etc (in other words, it
works). If I remove the third else condition in my RowCreated event,
the page works as expected form the get-go (only need to postback once
to have the update occur), but I obviously lose out on my pop-up
calendar.

Anyone got any clues what is going on here?
 
E

Evan M.

Here's my GridView and my SqlDataSource

<asp:GridView ID="ContactHistoryGrid" runat="server"
AutoGenerateColumns="False" DataSourceID="ContactHistoryDS"
DataKeyNames="JobHistoryID"
OnRowCreated="ContactHistoryGrid_RowCreated"
CssClass="GridViewTable"
GridLines="None" CellSpacing="1" CellPadding="3" AllowSorting="True"
AllowPaging="True">
<EmptyDataTemplate>
<div class="NoResultsPanel">
No items were found for your search.
</div>
</EmptyDataTemplate>
<PagerTemplate>
<div style="float: right;">
<asp:Button ID="NextButton" runat="server" Text="Next"
CommandName="Page" CommandArgument="Next" />
<asp:Button ID="LastButton" runat="server" Text="Last"
CommandName="Page" CommandArgument="Last" />
</div>
<div style="float: left;">
<asp:Button ID="FirstButton" runat="server" Text="First"
CommandName="Page" CommandArgument="First" />
<asp:Button ID="PrevButton" runat="server" Text="Prev"
CommandName="Page" CommandArgument="Prev" />
</div>
Page <asp:DropDownList ID="PageDropDown" runat="server"
AutoPostBack="true" />
of <asp:Literal ID="PageCountLiteral" runat="server" />

</PagerTemplate>
<PagerSettings Position="TopAndBottom" />
<PagerStyle CssClass="tableSmallHeaderCell" />
<Columns>
<asp:BoundField DataField="CustomerName" HeaderText="Company"
ReadOnly="True" SortExpression="CustomerName" >
<ItemStyle CssClass="tableDataCell" />
<HeaderStyle CssClass="tableSmallHeaderCell" />
</asp:BoundField>
<asp:BoundField DataField="Position" HeaderText="Position"
SortExpression="Position" >
<ItemStyle CssClass="tableDataCell" />
<HeaderStyle CssClass="tableSmallHeaderCell" />
</asp:BoundField>
<asp:TemplateField HeaderText="Start Date"
SortExpression="StartDate">
<EditItemTemplate>
<asp:TextBox ID="EditStartDateTextBox" runat="server" Text='<%#
Bind("StartDate", "{0:MMM d, yyyy}") %>' />
<asp:HyperLink ID="StartDateCalendarLink" runat="server"
ImageUrl="~/images/calendarButton.png" />
</EditItemTemplate>
<ItemTemplate>
<asp:Literal ID="StartDateLiteral" runat="server" Text='<%#
Eval("StartDate", "{0:MMM d, yyyy}") %>' />
</ItemTemplate>
<ItemStyle CssClass="tableDataCell" />
<HeaderStyle CssClass="tableSmallHeaderCell" />
</asp:TemplateField>
<asp:TemplateField HeaderText="EndDate" SortExpression="EndDate">
<EditItemTemplate>
<asp:TextBox ID="EditEndDateTextBox" runat="server" Text='<%#
Bind("EndDate", "{0:MMM d, yyyy}") %>' />
<asp:HyperLink ID="EndDateCalendarLink" runat="server"
ImageUrl="~/images/calendarButton.png" />
</EditItemTemplate>
<ItemTemplate>
<asp:Literal ID="EndDateLiteral" runat="server" Text='<%#
Eval("EndDate", "{0:MMM d, yyyy}") %>' />
</ItemTemplate>
<ItemStyle CssClass="tableDataCell" />
<HeaderStyle CssClass="tableSmallHeaderCell" />
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowEditButton="True">
<HeaderStyle CssClass="tableSmallHeaderCell" />
<ItemStyle CssClass="tableDataCell" />
</asp:CommandField>
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="ContactHistoryDS" runat="server"
ConnectionString="<%$ ConnectionStrings:SalesCRM %>" ProviderName="<%
$ ConnectionStrings:SalesCRM.ProviderName %>"
SelectCommand="SELECT ContactID, FirstName, LastName, JobHistoryID,
CustomerNumber, CustomerName, Position, StartDate, EndDate FROM
dbo.ContactJobHistory_V WHERE ContactID = @contactid AND EndDate IS
NOT NULL" SelectCommandType="text"
UpdateCommand="dbo.UpdateJob_usp"
UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="contactid"
QueryStringField="contactid" />
</SelectParameters>
</asp:SqlDataSource>

Here's my OnRowCreatedEvent:

protected void ContactHistoryGrid_RowCreated (object sender,
GridViewRowEventArgs e) {
GridView gv = (GridView) sender;
if (e.Row.RowType == DataControlRowType.Header) {
// Apply the sorting arrows to the header as needed.
GridViewFunctions.AddSortArrow (gv, e);
} else if (e.Row.RowType == DataControlRowType.Pager) {
// Setup the paging buttons (enable/disable the buttons, attach the
proper event function to the dropdownlist)
Button first = (Button) e.Row.Cells[0].FindControl ("FirstButton");
Button previous = (Button) e.Row.Cells[0].FindControl
("PrevButton");
Button next = (Button) e.Row.Cells[0].FindControl ("NextButton");
Button last = (Button) e.Row.Cells[0].FindControl ("LastButton");
DropDownList pageSelect = (DropDownList) e.Row.Cells[0].FindControl
("PageDropDown");
Literal pageCount = (Literal) e.Row.Cells[0].FindControl
("PageCountLiteral");
GridViewFunctions.InitializeButtonPager (gv, e, first, previous,
pageSelect, pageCount, next, last);
} else if (e.Row.RowType == DataControlRowType.DataRow &&
((e.Row.RowState == (DataControlRowState.Alternate |
DataControlRowState.Edit)) || e.Row.RowState ==
DataControlRowState.Edit)) {
// Set the proper javascript function for the image link to open
the popup calendar
string script = "javascript:eek:penCalendar('{0}', '{1}');";
HyperLink calLink = (HyperLink) e.Row.FindControl
("StartDateCalendarLink");
TextBox startDateTB = (TextBox) e.Row.FindControl
("EditStartDateTextBox");
DataRowView dataItem = (DataRowView) e.Row.DataItem;
if (dataItem != null) {
string startDateString = ((DateTime)
dataItem.Row.ItemArray[7]).ToString ("d-MMM-yyyy");
calLink.NavigateUrl = String.Format (script, startDateTB.ClientID,
Server.UrlEncode (startDateString));
}
}
}

The premise here is that in the row that gets edited, I have a textbox
with a link to open a pop-up calendar, that will write back the value
to the text box. That part works. The problem is when I try to click
the update button. The page postsback, but it doesn't leave the "edit"
mode. Instead, it comes back and all the textboxes are now blank. If I
type the values back into the textboxes and submit a second time, my
database gets updated, it leaves edit mode, etc (in other words, it
works). If I remove the third else condition in my RowCreated event,
the page works as expected form the get-go (only need to postback once
to have the update occur), but I obviously lose out on my pop-up
calendar.

Anyone got any clues what is going on here?

Alright, I have a little bit more information:
It appears that the call to ClientID is what is throwing everything
off. If I don't call the clientID property, then when I do a
viewsource on the page output (before I submit the update), the id for
the textbox is stated as "ctl00$MainContent$ContactHistoryGrid
$ctl04$EditStartDateTextBox" (as all the control within the edit row
are named).

However, if I do make the call to ClientID, then the ID of the textbox
BEFORE the first update postback is "EditStartDateTextBox" (and the
other controls are named similarily, missing the initial pre-amble).
When I do submit the update, the page comes back again still in edit
mode, but the textboxes are blank, and now all the clientIDs are set
to their preamble names.

It appears that calling ClientID from within the RowCreated event is
preventing some form of processing on the server side or something.

Anyone have any ideas abotu how I can work around this (short of doing
a manual setting of the ID including the preamble)?

Thanks,
Evan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top