473,383 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

GridView Update doesn't update on first postback

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:openCalendar('{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?

Apr 18 '07 #1
1 10350
On Apr 18, 9:07 am, "Evan M." <emorgoch.pub...@gmail.comwrote:
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:openCalendar('{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

Apr 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: nolan | last post by:
I have an asp.net 2.0 page with a gridview and detailsview on the same page set up in a master-details scenario. The gridview and detailsview have separate SQL data sources. The user enters...
1
by: jmdolinger | last post by:
Hi all, I'm a newbie to Atlas (and recently ASP.NET) after coming from a long Java background, also have done quite a bit with an Ajax.NET/ASP.NET 1.1 project, but it was basically all...
3
by: cpnet | last post by:
I have a GridView which I'm populating from an ObjectDataSource (give the GridView a DataTable). The GridView will have about 20 rows, and only one editable column. The editable column consists...
0
by: =?Utf-8?B?TGFkaXNsYXYgTXJua2E=?= | last post by:
Hello, I read some msdn and other articles about how does databinding among DataSource controls and FormView / GridView controls works but I still don't fully understand to this blackbox. I have...
8
by: gerry | last post by:
The PagerSettings.Visible property is not being handled properly by the GridView control. The value assigned to this property is not applied until after a postback. Simplest test : .aspx...
4
by: Mark Olbert | last post by:
I'm running into a well-described issue in the ASPNET model that I haven't found a good work around for. I have a GridView to which I dynamically add data-bound TemplateFields at run-time. The...
0
by: =?Utf-8?B?Sm9l?= | last post by:
I have a gridview with buttons in a column and I want those buttons to be triggers for an update panel further down on the page. So for example, when a user clicks on the button in row 3, I want...
11
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know...
4
by: BiffMaGriff | last post by:
Hello, I have a GridView that I put inside an update panel. I have a control that attaches to the datasource of the gridview that filters the data, databinds the gridview and then updates the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.