enter key causing postback and then errors

  • Thread starter Thread starter SAL
  • Start date Start date
S

SAL

Hello,
Using VS2005, AJAX toolkit.

I have a page that has 2 update panels on it. The top Updatepanel has a
gridview (gvStormDetail) in it and the bottom one has a details view
(dvInsert).
In gvStormDetail, there's a textbox in a column for user input. There's a
button control in this updatepanel for updating this page of gridview rows
all at once.

In dvInsert there's a dropdownlist control that gets updated with values
when the user selects a category type from a dropdown, in this updatepanel,
but outside of the detailsview.

The problem is that when a user changes some text in gvStormDetail and then
hits the Enter Key, a postback occurs and the gridview now shows the first
row as a selected row. Then, if the user clicks the button for updating the
values in this gridview, a message box appears with the following text in
it.

'ddlStormAssetE' has a SelectedValue which is invalid because it does not
exist in the list of items. Parameter name: value

So, I have some questions:
1. Is there a way to keep this postback from occuring when the user hit the
enter key in this textbox inside a row in gvStormDetail?
2. I have not be able to find out the event that's firing and how could I do
that?
3. How can I keep the first row of the gridview from being selected?

Also, this message appears no matter what control is clicked on after this
happens so the page has to be left and come back to before it behaves
correctly again. I'll post the html source if it might help.

<%@ Page Language="VB" MasterPageFile="~/Master/GISMasterPage.master"
AutoEventWireup="false" CodeFile="StormDetails.aspx.vb"
Inherits="Public_StormDetails" title="Storm Edit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent"
Runat="Server">
<asp:Panel ID="pnlHeader" runat="server" EnableViewState="False"
HorizontalAlign="Center"
Width="100%">
<asp:DetailsView ID="dvHeader" runat="server"
AutoGenerateRows="False" DataKeyNames="CntyAssetID"
DataSourceID="dsHeader" SkinID="dvplainSkin" Width="328px">
<Fields>
<asp:BoundField DataField="CntyAssetID" HeaderText="County
Asset ID" InsertVisible="False"
ReadOnly="True" SortExpression="CntyAssetID">
<HeaderStyle Font-Bold="True" HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name">
<HeaderStyle Font-Bold="True" HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="Bkpg" HeaderText="Book page"
SortExpression="Bkpg">
<HeaderStyle Font-Bold="True" HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="TotalValue"
DataFormatString="{0:C0}" HeaderText="Total Value"
HtmlEncode="False" SortExpression="TotalValue">
<HeaderStyle Font-Bold="True" HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="RecordingDate"
DataFormatString="{0:MM/dd/yyyy}" HeaderText="Recording Date"
HtmlEncode="False" SortExpression="RecordingDate">
<HeaderStyle Font-Bold="True" HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
</Fields>
<HeaderStyle Font-Bold="False" />
</asp:DetailsView>
<asp:ObjectDataSource ID="dsHeader" runat="server"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetBundleHeaderById" TypeName="BundleHeaderBLL">
<SelectParameters>
<asp:SessionParameter Name="id" SessionField="cntyAssetId"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</asp:Panel>
<br />
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="gridviewDiv" runat="server" style="border-color:#EFEFEF;
border-style:solid; border-width:2px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server"
EnableViewState="False">
<ContentTemplate>
<asp:GridView ID="gvStormDetail"
runat="server"
AutoGenerateColumns="False"
DataSourceID="dsStormDetail"
EnableViewState="False"
SkinID="gridviewSkin"
Width="672px"
DataKeyNames="StormValueID"
AllowSorting="True">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="lbEdit"
runat="server"
CausesValidation="False"
CommandName="Select"
ImageUrl="~/Images/edit_white2.gif"
CommandArgument="EditMe" EnableViewState="False">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton ID="lbDelete"
runat="server"
CausesValidation="False"
CommandName="Delete"
OnClientClick="return confirm('Are you sure
you want to delete this Storm Record?');"
ImageUrl="~/Images/Delete.gif"
ToolTip="Delete this record permanently"
EnableViewState="False">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="hlMap" runat="server"
Target="_map">Map</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StormValueID"
HeaderText="StormValueID" InsertVisible="False"
ReadOnly="True" SortExpression="StormValueID"
Visible="False" />
<asp:BoundField DataField="StormAssetType"
HeaderText="Asset Category" SortExpression="StormAssetType" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="StormAssetTypeID"
HeaderText="StormAssetTypeID" SortExpression="StormAssetTypeID"
Visible="False" />
<asp:TemplateField HeaderText="Storm Asset Name"
SortExpression="StormAssetName">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server"
Text='<%# Bind("StormAssetName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("StormAssetName") %>' Width="200px"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="StmFacilityClkID"
SortExpression="StmFacilityClkID">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"
Text='<%# Bind("StmFacilityClkID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblStmFacilityClkId"
runat="server" Text='<%# Bind("StmFacilityClkID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Description"
SortExpression="Description">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server"
EnableViewState="False" Height="24px"
Text='<%# Bind("Description") %>'
Width="232px"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Estimated Value"
SortExpression="EstValue">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("EstValue") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblEstValue" runat="server"
Text='<%# Bind("EstValue", "{0:C0}") %>'></asp:Label>
<asp:TextBox ID="txtEstValue" runat="server"
EnableViewState="False" Text='<%# Bind("EstValue", "{0:C0}") %>'
Visible="False" Width="96px"
OnTextChanged="txtEstValue_TextChanged"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:BoundField DataField="CntyAssetID"
HeaderText="CntyAssetID" SortExpression="CntyAssetID"
Visible="False" />
<asp:TemplateField></asp:TemplateField>
<asp:BoundField DataField="ModUser" HeaderText="ModUser"
SortExpression="ModUser">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ModDate"
DataFormatString="{0:MM/dd/yyyy}" HeaderText="ModDate"
HtmlEncode="False" SortExpression="ModDate">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
</asp:GridView>
<br />
<asp:ObjectDataSource ID="dsStormDetail" runat="server"
SelectMethod="GetStormAssetsByCntyAssetdId"
TypeName="StormAssetsMainBLL"
EnableViewState="False"
DeleteMethod="Delete">
<SelectParameters>
<asp:SessionParameter Name="id"
SessionField="cntyAssetId" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="stormValueId" Type="Int32" />
</DeleteParameters>
</asp:ObjectDataSource>
<table id="tblTotalValue" style="font-family:Arial;
font-size:10pt" width="100%">
<tr>
<td style="width:25%">
<asp:Label ID="lblTotalLabel" runat="server"
Font-Names="Arial" Font-Size="10pt"
Text="Total Estimated Value:"
Width="168px"></asp:Label></td>
<td style="width:25%"></td>
<td style="width:25%"></td>
<td style="width:25%">
<asp:Label ID="lblTotal" runat="server"
Font-Names="Arial" Font-Size="10pt" Text="Total Value:"
Width="96px"></asp:Label></td>
</tr>
</table>
<br />
<asp:Button ID="btnUpdate" runat="server"
EnableViewState="False"
Text="Save Changes" OnClick="btnUpdate_Click"
CausesValidation="False" ToolTip="Saves your Estimated Value changes"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="dvStormInsert"
EventName="ItemInserting" />
</Triggers>
</asp:UpdatePanel>

</div>

<br />
<br />
<div>
<br />
<asp:UpdatePanel ID="upInsert" runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblAssetType" runat="server"
EnableViewState="False" Font-Size="10pt"
Text="Category" Width="104px" BackColor="#EFEFEF"
Height="18px"></asp:Label><asp:DropDownList ID="ddlAssetType" runat="server"
AutoPostBack="True"
DataSourceID="dsAssetTypes"
DataTextField="StormAssetType"
DataValueField="StormAssetTypeID"
Width="236px" EnableViewState="False">
</asp:DropDownList>
<asp:ObjectDataSource ID="dsAssetTypes" runat="server"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetAssetTypesPlusNone"
TypeName="StormAssetTypeBLL"
EnableViewState="False">
</asp:ObjectDataSource>
<br />
<asp:DetailsView ID="dvStormInsert"
runat="server"
DefaultMode="Insert"
EnableViewState="False"
Height="50px"
Width="368px"
SkinID="detailsviewSkin"
AutoGenerateRows="False"
DataKeyNames="StormValueID"
DataSourceID="dsStormInsert" >
<Fields>
<asp:TemplateField HeaderText="StormValueID"
InsertVisible="False" SortExpression="StormValueID">
<EditItemTemplate>
<asp:Label ID="lblStormValueId" runat="server"
Text='<%# Eval("StormValueID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%#
Bind("StormValueID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CntyAssetID"
SortExpression="CntyAssetID">
<EditItemTemplate>
<asp:Label ID="lblCntyAssetId" runat="server"
Text='<%# Eval("CntyAssetID") %>' Width="184px"></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtCntyAssetId" runat="server"
Enabled="False" EnableViewState="False"
Text='<%# Bind("CntyAssetID") %>'
Width="230px"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("CntyAssetID") %>' Width="176px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Storm Asset"
SortExpression="StormAssetID">
<EditItemTemplate><asp:DropDownList
ID="ddlStormAssetE"
runat="server"
DataSourceID="dsStormAssetE"
DataTextField="StormAssetName"
DataValueField="StormAssetID"
Width="236px" SelectedValue='<%#
Bind("StormAssetID") %>'>
</asp:DropDownList><br />
<asp:ObjectDataSource ID="dsStormAssetE"
runat="server"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetStormAssetsByAssetTypeId"
TypeName="StormAssetsBLL">
<SelectParameters>
<asp:ControlParameter
ControlID="ddlAssetType"
Name="id"
PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="ddlStormAsset"
runat="server"
DataSourceID="dsStormAsset"
DataTextField="StormAssetName"
DataValueField="StormAssetID"
Width="236px" EnableViewState="False">
</asp:DropDownList>
<asp:ObjectDataSource ID="dsStormAsset"
runat="server"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetStormAssetsByAssetTypeId"
TypeName="StormAssetsBLL">
<SelectParameters>
<asp:ControlParameter
ControlID="ddlAssetType"
Name="id"
PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#
Bind("StormAssetID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description"
SortExpression="Description">
<EditItemTemplate>
<asp:TextBox ID="txtDescriptionE" runat="server"
EnableViewState="False" Text='<%# Bind("Description") %>'
TextMode="MultiLine"
Width="230px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtDescription" runat="server"
Text='<%# Bind("Description") %>' EnableViewState="False"
TextMode="MultiLine" Width="230px"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#
Bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Estimated Value"
SortExpression="EstValue">
<EditItemTemplate>
<asp:TextBox ID="txtEstValueE" runat="server"
Text='<%# Bind("EstValue") %>' Width="230px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtEstValue" runat="server"
Text='<%# Bind("EstValue") %>' Width="230px"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#
Bind("EstValue") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StmFacilityClkID"
SortExpression="UniqueID">
<EditItemTemplate>
<asp:TextBox ID="txtUniqueIdE" runat="server"
Text='<%# Bind("StmFacilityClkID") %>'
Width="230px"></asp:TextBox><asp:RequiredFieldValidator
ID="rfvUniqueIdE" runat="server"
ControlToValidate="txtUniqueIdE"
EnableViewState="False" ErrorMessage="StmFacilityClkID is a required field"
EnableClientScript="False">*</asp:RequiredFieldValidator>
<asp:CustomValidator ID="cvStmFacilityClkIdE"
runat="server" ControlToValidate="txtUniqueIdE"
ErrorMessage="StmFacilityClkID is not valid"
EnableViewState="False"
OnServerValidate="ServerValidateID">*</asp:CustomValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtUniqueId" runat="server"
Text='<%# Bind("StmFacilityClkID") %>'
Width="230px"></asp:TextBox><asp:RequiredFieldValidator ID="rfvUniqueId"
runat="server" EnableViewState="False"
ErrorMessage="StmFacilityClkID is a required
field" ControlToValidate="txtUniqueId" Display="Dynamic"
EnableClientScript="False">*</asp:RequiredFieldValidator>
<asp:CustomValidator ID="cvStmFacilityClkIdI"
runat="server"
ControlToValidate="txtUniqueId"
EnableViewState="False" ErrorMessage="StmFacilityClkID is not valid"
OnServerValidate="ServerValidateID">*</asp:CustomValidator>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%#
Bind("StmFacilityClkID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<InsertItemTemplate>
<asp:LinkButton ID="lbInsert" runat="server"
CausesValidation="True"
CommandName="Insert"
Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="lbCancel" runat="server"
CausesValidation="False"
CommandName="Cancel"
OnClick="lbCancel_Click"
Text="Cancel"></asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:ObjectDataSource ID="dsStormInsert" runat="server"
EnableViewState="False" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetStormValueByStormValueId" TypeName="StormValueBLL"
DeleteMethod="Delete" UpdateMethod="UpdateStormTable">
<DeleteParameters>
<asp:Parameter Name="stormValueid" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:SessionParameter Name="id"
SessionField="stormValueId" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
EnableViewState="False" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlAssetType"
EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>

</div>
<br />
</asp:Content>
 
After further investigation, it looks like the imagebutton I have on each
row of gvStormDetail is doing the posting back. This button's command is
select which is what is selecting the row. When a row gets selected in this
fashion, I'm populating the detailsview so the user can edit it. The
question is, how do I keep this from happening when a user presses the enter
key on their keyboard?
This page has a masterpage as well as you can probably see by viewing it's
source html below.

If this isn't it, I'm more confused than I previously thought... :)
 
Hi SAL,

Currently I could only provide a solution to your first question:

Quote from SAL==================================================
1. Is there a way to keep this postback from occuring when the user hit the
enter key in this textbox inside a row in gvStormDetail?
==================================================

We can add following JavaScript on your page to disable the enter key.

<script type="text/javascript">

function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement
: null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}

document.onkeypress = stopRKey;

</script>

To answer your other questions I think I need to get a demo from you. I'll
try to reproduce it on my side and see what the best way is to solve this
issue. My email is (e-mail address removed). Please update here after sending
the project in case I missed that email.

Regards,
Allen Chen
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Allen,
I'm putting together a repro project for this issue, from my 2nd post above.
I will post when I have it ready.

S
 
Actually, I solved this problem with the following code in the pageload
event:
Form.DefaultButton = btnUpdate.UniqueID


S
 
Hi SAL,

Glad to know you've solved this issue by yourself and thanks for sharing
the solution with us.

Have a nice day!
Allen Chen
Microsoft Online Community Support
 
Back
Top