TextBoxes in UpdatePanel are causing PageRequestManagerServerErrorException - c#

I'm trying to create a page that has some information in databound labels with and edit button. When the edit button is clicked, the information is replaced with TextBoxes bound to the same data. The data can then be modified, saved back to the DB and the TextBoxes replaced with updated Labels.
To start with, and to keep things simple, all I have is an UpdatePanel with a DataList and two buttons: EditButton and CancelButton (CancelButton is hidden by default). The DataList's ItemTemplate has two Panels: ViewPanel and EditPanel (EditPanel is hidden by default). When EditButton is clicked, I hide EditButton and the DataList's Items' ViewPanel, and show CancelButton and the DataList's Items' EditPanel.
Not a problem. Once this is done however, the CancelButton button will not work, throwing a PageRequestManagerServerErrorException.
Through some fiddling, I worked out this happens because there are databound text boxes on the EditPanel. If I don't bind data to the text boxes, everything works perfectly. Why doesn't this work?
Here's my code:
UpdatePanelTest.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="UpdatePanelTest.aspx.cs" Inherits="WebLetterViewer.UpdatePanelTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
<asp:SqlDataSource ID="AllLettersDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ORMSTestConnectionString %>"
SelectCommand="SELECT * FROM [Letters] WHERE ([id] = #id)">
<SelectParameters>
<asp:ControlParameter ControlID="HiddenLetterID" DefaultValue="1" Name="id" PropertyName="Value" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:HiddenField ID="HiddenLetterID" runat="server" Value="1" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:DataList ID="LettersDataList" runat="server" DataSourceID="AllLettersDataSource">
<ItemTemplate>
<asp:Panel ID="ViewPanel" runat="server">
<h2>Data1:</h2>
<asp:Label ID="data1Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" Text='<%# Eval("data1") %>' Width="500px" />
<h2>data2:</h2>
<asp:Label ID="data2Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" Text='<%# Eval("data2") %>' Width="500px" />
<h2>data3:</h2>
<asp:Label ID="data3Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px" Text='<%# Eval("data3") %>' Width="500px" />
</asp:Panel>
<asp:Panel ID="EditPanel" runat="server" Visible="False">
<h2>data1:</h2>
<asp:TextBox ID="data1TextBox" runat="server" Height="100px" Text='<%# Eval("data1", "{0}") %>' TextMode="MultiLine" Width="500px"></asp:TextBox>
<h2>data2:</h2>
<asp:TextBox ID="data2TextBox" runat="server" Height="100px" Text='<%# Eval("data2", "{0}") %>' TextMode="MultiLine" Width="500px"></asp:TextBox>
<h2>data3:</h2>
<asp:TextBox ID="data3TextBox" runat="server" Height="100px" Text='<%# Eval("data3", "{0}") %>' TextMode="MultiLine" Width="500px"></asp:TextBox>
</asp:Panel>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="EditButton" runat="server" onclick="EditButton_Click" Text="Edit" />
<asp:Button ID="CancelButton" runat="server" onclick="CancelButton_Click" Text="Cancel" Visible="False" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
UpdatePanelTest.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebLetterViewer{
public partial class UpdatePanelTest : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){
}
protected void EditButton_Click(object sender, EventArgs e){
foreach (DataListItem item in LettersDataList.Items){
item.FindControl("ViewPanel").Visible = false;
item.FindControl("EditPanel").Visible = true;
}
EditButton.Visible = false;
CancelButton.Visible = true;
UpdatePanel1.Update();
}
protected void CancelButton_Click(object sender, EventArgs e){
foreach (DataListItem item in LettersDataList.Items){
item.FindControl("ViewPanel").Visible = true;
item.FindControl("EditPanel").Visible = false;
}
EditButton.Visible = true;
CancelButton.Visible = false;
UpdatePanel1.Update();
}
}
}

Put your EditPanel in a EditItemTemplate and use Commands , you are not using this control the way it was designed to be used:
How to: Allow Users to Edit Items in DataList Web Server Controls
Markup:
<asp:SqlDataSource ID="AllLettersDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORMSTestConnectionString %>"
SelectCommand="SELECT * FROM [Letters] WHERE ([id] = #id)">
<SelectParameters>
<asp:ControlParameter ControlID="HiddenLetterID" DefaultValue="1" Name="id" PropertyName="Value"
Type="Int32" />
</SelectParameters>
<!--change this -->
UpdateCommand="UPDATE [Categories] SET [CategoryName] = #CategoryName, [Description]
= #Description WHERE [CategoryID] = #original_CategoryID">
<UpdateParameters>
<asp:Parameter Name="CategoryName" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="original_CategoryID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:DataList ID="LettersDataList" runat="server" DataSourceID="AllLettersDataSource"
OnEditCommand="LettersDataList_EditCommand" OnCancelCommand="LettersDataList_CancelCommand"
OnUpdateCommand="LettersDataList_UpdateCommand">
<ItemTemplate>
<asp:Panel ID="ViewPanel" runat="server">
<h2>
Data1:</h2>
<asp:Label ID="data1Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px"
Text='<%# Eval("data1") %>' Width="500px" />
<h2>
data2:</h2>
<asp:Label ID="data2Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px"
Text='<%# Eval("data2") %>' Width="500px" />
<h2>
data3:</h2>
<asp:Label ID="data3Label" runat="server" BorderStyle="Solid" BorderWidth="1px" Height="100px"
Text='<%# Eval("data3") %>' Width="500px" />
</asp:Panel>
<asp:Button ID="EditButton" runat="server" CommandName="edit" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Panel ID="EditPanel" runat="server">
<h2>
data1:</h2>
<asp:TextBox ID="data1TextBox" runat="server" Height="100px" Text='<%# Eval("data1", "{0}") %>'
TextMode="MultiLine" Width="500px"></asp:TextBox>
<h2>
data2:</h2>
<asp:TextBox ID="data2TextBox" runat="server" Height="100px" Text='<%# Eval("data2", "{0}") %>'
TextMode="MultiLine" Width="500px"></asp:TextBox>
<h2>
data3:</h2>
<asp:TextBox ID="data3TextBox" runat="server" Height="100px" Text='<%# Eval("data3", "{0}") %>'
TextMode="MultiLine" Width="500px"></asp:TextBox>
</asp:Panel>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="update">
Save
</asp:LinkButton>
<asp:Button ID="CancelButton" runat="server" CommandName="cancel" Text="Cancel" Visible="False" />
</EditItemTemplate>
</asp:DataList>
Code-behind:
protected void LettersDataList_EditCommand(object source, DataListCommandEventArgs e)
{
LettersDataList.EditItemIndex = e.Item.ItemIndex;
LettersDataList.DataBind();
}
protected void LettersDataList_CancelCommand(object source,
DataListCommandEventArgs e)
{
LettersDataList.EditItemIndex = -1;
LettersDataList.DataBind();
}
protected void LettersDataList_UpdateCommand(object source,
DataListCommandEventArgs e)
{
//change this to your database needs
//String categoryID =
// LettersDataList.DataKeys[e.Item.ItemIndex].ToString();
//String categoryName =
// ((TextBox)e.Item.FindControl("textCategoryName")).Text;
//String description =
// ((TextBox)e.Item.FindControl("textDescription")).Text;
//SqlDataSource1.UpdateParameters["original_CategoryID"].DefaultValue
// = categoryID;
//SqlDataSource1.UpdateParameters["categoryName"].DefaultValue
// = categoryName;
//SqlDataSource1.UpdateParameters["Description"].DefaultValue
// = description;
//SqlDataSource1.Update();
LettersDataList.EditItemIndex = -1;
LettersDataList.DataBind();
}

Related

How to correctly get cell value in gridview after clicking on a linkbutton inside a gridview and enable correct async postback to display a modal

I am making a reservation system and currently stuck on the following situation.
Basically I have a GridView which is loaded with the reservation ID and two LinkButton which opens a modal popup to enable the customer to view the details.
What I want to achieve is that when I click any LinkButton, it will show the ID from the first cell of a GridViewRow.
I have tried these methods here, here, here, and here but it still doesn't work. Is there any way to do it, Aside from using a SELECT Command and does involve async postback or if async postback is not possible with this, any fix recommended?
Here is my GridView code:
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gvReservationSummaries"/>
</Triggers>
<ContentTemplate>
<h2>Reservations</h2>
<br />
<asp:GridView ID="gvReservationSummaries" runat="server"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
AllowPaging="True" ShowHeader="False" CellPadding="5" CellSpacing="5"
onrowcommand="Gridview1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"><%# Eval("Master_Reservation_ID")%></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="ViewReservationDetails" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'>View Reservation Details</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="ViewReceiptDetails" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'>View Receipt Details</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DB_9E4ABD_GratchiOBRSConnectionString2 %>"
SelectCommand="SELECT [Master_Reservation_ID] FROM [Master_Reservation] WHERE User_Unique_ID = #uuid">
<SelectParameters>
<asp:SessionParameter Name="uuid" SessionField="uid" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:Button ID="btnShowModal" runat="server" Text="Show Modal 1 (Debug Purposes Only)" onclick="btnShowModal_Click" />
<!--Here, put the details of the reservaton, plus options to edit reservation.-->
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
<asp:Panel ID="reservationSummaryModal" runat="server" Width="500px" Height="500px" style="display: none; background-color:Black; color:#CCC;">
<asp:Button ID="btnExitRs" runat="server" Text="Hide Modal (Debug Purposes Only)" onclick="btnExitAct_Click" />
<asp:Label ID="lblReservationSummary" runat="server" Text="Label"></asp:Label>
</asp:Panel>
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="HiddenField1" PopupControlID="reservationSummaryModal" BackgroundCssClass="modalBg" CancelControlID="btnExitRs">
</asp:ModalPopupExtender>
<asp:HiddenField ID="HiddenField1" runat="server" />
And here is my codebehind:
protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ViewReservationDetails")
{
//GridViewRow gvr = gvReservationSummaries.Rows[Convert.ToInt32(e.CommandArgument)];
gvReservationSummaries.SelectedIndex = Convert.ToInt32(e.CommandArgument);
GridViewRow gvr = gvReservationSummaries.Rows[gvReservationSummaries.SelectedIndex];
lblReservationSummary.Text = "The id is: " + gvr.Cells[0].Text.ToString();
ModalPopupExtender1.Show();
}
else if (e.CommandName == "ViewReceiptDetails")
{
gvReservationSummaries.SelectedIndex = Convert.ToInt32(e.CommandArgument);
GridViewRow gvr = gvReservationSummaries.Rows[gvReservationSummaries.SelectedIndex];
lblReservationSummary.Text = "The id is: " + gvr.Cells[0].Text.ToString();
ModalPopupExtender1.Show();
}
else
{
}
}

ListView inside an UpdatePanel not getting rebound

I have a ASP.NET ListView inside an UpdatePanel.
In the ItemTemplate there are 2 buttons with Commands "UpdateCart" & "RemoveCart".
In both the commands, the datasource of ListView (an SQL DataSource) gets rebound.
There is no InsertCommand/UpdateCommand/DeleteCommand on the SqlDataSource
My problem is :
Whenver these buttons are clicked : the datasource does not get refreshed.
however I can see from debug mode that values are indeed passed to the datasource selecting method correctly.
The relevant ASPX Page :
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdateProgress ID="UpdateProgress1" DisplayAfter="10" runat="server" AssociatedUpdatePanelID="UpnlMain">
<ProgressTemplate>
<div class="divWaiting">
<asp:Label ID="lblWait" runat="server" Text=" Please wait... " />
<asp:Image ID="imgWait" runat="server" ImageAlign="Middle" ImageUrl="~/Image/AjaxLoading.gif" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpnlMain" runat="server">
<ContentTemplate>
<asp:ListView ID="lvMain" runat="server" DataSourceID="sdsMain" DataKeyNames="Code"
OnItemCommand="lvMain_ItemCommand">
<EmptyDataTemplate>
<div class="col-xs-12">
<span>No Items in Cart.</span>
</div>
</EmptyDataTemplate>
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# Eval("Code") %>' Visible="false" />
Product " <asp:Literal ID="ltrStyleNo" runat="server" Text='<%# Eval("StyleNo") %>' />
Qty : <asp:TextBox ID="txtQty" runat="server" type="number" CssClass="txtQty OnlyNumeric form-control text-center input-sm"
Text='<%# Eval("Qty") %>'></asp:TextBox>
<asp:ImageButton ID="btnUpdate" runat="server" ImageUrl="~/image/update.png" CssClass="btn btnUpdateCart"
CommandName="UpdateCart" AlternateText="Update Qty" />
<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/image/delet.png" CssClass="btn"
CommandName="DeleteCart" AlternateText="Remove Item" />
</ItemTemplate>
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
</asp:ListView>
<asp:SqlDataSource ID="sdsMain" runat="server" ConnectionString="<%$ ConnectionStrings:CurrentConnectionString %>"
SelectCommand="pGetItemsForCart" SelectCommandType="StoredProcedure" OnSelecting="sdsMain_Selecting"
UpdateCommand="NoUpdate" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter DefaultValue="-1" Name="WoSCodeCSV" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:Parameter DefaultValue="-1" Name="WoSCodeCSV" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
The Code Behind Code :
protected void sdsMain_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters[0].Value = GetCartCodes(); //gives some values in csv from Session ..
}
protected void lvMain_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName.ToLower() == "UpdateCart".ToLower())
{
Label lblCode = e.Item.FindControl("lblCode") as Label;
TextBox txtQty = e.Item.FindControl("txtQty") as TextBox;
//Some update in a Session
BindListView();
}
else if (e.CommandName.ToLower() == "DeleteCart".ToLower())
{
Label lblCode = e.Item.FindControl("lblCode") as Label;
//Some update in a Session
BindListView();
}
else if (e.CommandName.ToLower() == "RefreshCart".ToLower())
{
Label lblCode = e.Item.FindControl("lblCode") as Label;
BL.StyleSearchHelper.PriceItem(Page, lblCode.Text);
BindListView();
}
}
void BindListView()
{
sdsMain.DataBind();
lvMain.DataBind();
}
Try replace your buttons outside the updatepanel. Page will not getting refresh when the buttons are in updatepanel. sorry i can't make comment so I post here as an answer.

Get values from dropdown menu's, run query and add values to formview c#

I have a asp.net webform which contains a formView, 3 dropdown menus and a submit button. The dropdown menus get their values from the database.
When the user clicks the submit button values from the dropdown menus should be run through our query and display the outcome in the formView. This is not happening.
When we give standard values for other, meat and vegetables in the callSelectProduct() we can see the correct output in the form view, but this is on page load.
This is the click method from the submit button:
protected void getRecipe(object sender, EventArgs e)
{
string ddlOther = DropDownOther.SelectedValue;
string ddlVegetables = DropDownVegetables.SelectedValue;
string ddlMeat = DropDownMeat.SelectedValue;
int ddlIntOther = int.Parse(ddlOther);
int ddlIntVegetables = int.Parse(ddlVegetables);
int ddlIntMeat = int.Parse(ddlMeat);
Business.Class1.callSelectProduct(ddlIntOther, ddlIntMeat, ddlIntVegetables);
}
This is callSelectProduct():
The Debug.WriteLine gives the good values back in the debug console, but then the page reloads because of the submit button click and then the Debug.WriteLine gives 0 0 0 back. I think that's why I don't see anything in the FormView. Because the combination of 0 0 0 will not return anything.
public static void callSelectProduct(int other, int meat, int vegetables)
{
SelectProduct(other, meat, vegetables);
}
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
public static Data.SouthWind.SelectRecipesFromIngredientsDataTable SelectProduct(int otherGet, int meatGet, int vegetablesGet)
{
int other = otherGet;
int meat = meatGet;
int vegetables = vegetablesGet;
System.Diagnostics.Debug.WriteLine("This is class 1 Other " + other + " Vegetable " + vegetables + " Meat " + meat);
DataAccess.SouthWindTableAdapters.SelectRecipesFromIngredientsTableAdapter tableAdaptertest = new DataAccess.SouthWindTableAdapters.SelectRecipesFromIngredientsTableAdapter();
return tableAdaptertest.GetData(other, meat, vegetables);
}
This is our webform:
<form id="form1" runat="server">
<div>
<asp:FormView ID="RecipeFormView" runat="server" AllowPaging="True" DataSourceID="ObjectDataSource1">
<EditItemTemplate>
RecipeName:
<asp:TextBox ID="RecipeNameTextBox" runat="server" Text='<%# Bind("RecipeName") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
RecipeName:
<asp:TextBox ID="RecipeNameTextBox" runat="server" Text='<%# Bind("RecipeName") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
RecipeName:
<asp:Label ID="RecipeNameLabel" runat="server" Text='<%# Bind("RecipeName") %>' />
<br />
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="SelectProduct" TypeName="Business.Class1">
<SelectParameters>
<asp:Parameter Name="otherGet" Type="Int32" />
<asp:Parameter Name="meatGet" Type="Int32" />
<asp:Parameter Name="vegetablesGet" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:DropDownList ID="DropDownOther" runat="server" DataSourceID="ObjectDataSource2" DataTextField="IngredientName" DataValueField="IngredientId">
</asp:DropDownList>
<asp:DropDownList ID="DropDownVegetables" runat="server" DataSourceID="SelectVegetables" DataTextField="IngredientName" DataValueField="IngredientId" Height="16px">
</asp:DropDownList>
<asp:DropDownList ID="DropDownMeat" runat="server" DataSourceID="SelectMeat" DataTextField="IngredientName" DataValueField="IngredientId">
</asp:DropDownList>
<asp:ObjectDataSource ID="SelectMeat" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="SelectMeat" TypeName="Business.Class1"></asp:ObjectDataSource>
<asp:ObjectDataSource ID="SelectVegetables" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="SelectVegetables" TypeName="Business.Class1"></asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="SelectOther" TypeName="Business.Class1"></asp:ObjectDataSource>
<asp:Button ID="Button1" runat="server" OnClick="getRecipe" Text="Button" UseSubmitBehavior="False" />
<br />
</div>
</form>
Any help is welcome!
Are you use !IsPostBack in outer of you dropdown binding method in inside of pageload event ?
for
void page-load()
{
if(!IsPostBack)
{
//Call all dropdownlist binding method
}
}

ASP.NET C# GridView - Focus on RowEditing

I have a large dataset binding to a gridview that is inside of a 300px height restricted DIV tag. When I select a row and trigger the edit function the page reloads the data (as desired) into the gridview and takes me back to the top regardless of how far I may have scrolled down.
Is there a way ideally only using asp.net c# to focus on the row that is being edited. I don't mind if this involves the row being placed at the top of the grid while in edit mode if that is required.
Below is CountryGrid.aspx
<%# Page Language="C#" MasterPageFile="~/MasterPageSimple.master" AutoEventWireup="true"
CodeFile="CountryGrid.aspx.cs" EnableEventValidation="false" Inherits="CountryGrid"
Title="JFA Admin Portal - OP49 Country Update" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<center>
<div class="headlanding">
<asp:Label ID="lb_Welcome" runat="server" CssClass="labelLargeFont" Text="Label">OP49 - Country Update</asp:Label>
<br />
<br />
<br />
<asp:Label ID="Label4" runat="server" CssClass="labelLargeFont" >You are currently modifying: </asp:Label>
<asp:Label ID="Country" runat="server" CssClass="labelLargeFont" />
<br />
<asp:Label ID="Label1" runat="server" CssClass="labelLargeFont" >For </asp:Label>
<asp:Label ID="Season" runat="server" CssClass="labelLargeFont" />
<br />
<br />
</div>
<br />
<div style="text-align: center">
<asp:Label ID="Error_Dashboard" runat="server" Font-Size="Small" ForeColor="#FF3300"></asp:Label>
<asp:Label ID="ErrorAccess" runat="server" Font-Size="Small" ForeColor="#FF3300"></asp:Label>
</center>
</div>
<asp:Panel ID="Panel1" runat="server" Font-Size="Large" CssClass="Grid_Panel" GroupingText="OP49 - Update Grid" Font-Bold="true">
<center>
<div style="max-height: 550px; overflow: auto;">
<asp:GridView ID="CountryGridView" DataKeyNames="i_SK_Accom" runat="server" Postback="False"
AutoGenerateColumns="False" CssClass="mGrid" RowStyle-CssClass="normal" Width="95%"
OnRowEditing="CountryGridView_RowEditing"
OnRowCancelingEdit="CountryGridView_RowCancelingEdit"
OnRowUpdating="CountryGridView_RowUpdating" >
<Columns>
<asp:TemplateField HeaderText="Accom Code">
<ItemTemplate>
<asp:Label ID="lb_Accom_Code" runat="server" Text='<%# Bind("Accom_Code") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lbl_Accom_Code" runat="server" Text='<%# Bind("Accom_Code") %>' />
</EditItemTemplate>
<HeaderStyle Width="80px" />
<ItemStyle Width="80px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Accom Name">
<ItemTemplate>
<asp:Label ID="lb_Accom_Name" runat="server" Text='<%# Bind("Accom_Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lbl_Accom_Name" runat="server" Text='<%# Bind("Accom_Name") %>' />
</EditItemTemplate>
<HeaderStyle Width="200px" />
<ItemStyle Width="200px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="OP49 Required?">
<EditItemTemplate>
<asp:DropDownList ID="txt_OP49" runat="server" SelectedValue='<%# Bind("OP49_Required") %>' CssClass="DropBoxYN">
<asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
<asp:ListItem Text="No" Value="N"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lb_OP49" runat="server" Text='<%# Bind("OP49_Required") %>' />
</ItemTemplate>
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Weekly">
<EditItemTemplate>
<asp:DropDownList ID="txt_Weekly" runat="server" SelectedValue='<%# Bind("Weekly") %>' CssClass="DropBoxYN">
<asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
<asp:ListItem Text="No" Value="N"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lb_Weekly" runat="server" Text='<%# Bind("Weekly") %>' />
</ItemTemplate>
<HeaderStyle Width="60px" />
<ItemStyle Width="60px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Daily">
<EditItemTemplate>
<asp:DropDownList ID="txt_Daily" runat="server" SelectedValue='<%# Bind("Daily") %>' CssClass="DropBoxYN">
<asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
<asp:ListItem Text="No" Value="N"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lb_Daily" runat="server" Text='<%# Bind("Daily") %>' />
</ItemTemplate>
<HeaderStyle Width="60px" />
<ItemStyle Width="60px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Actions" >
<ItemTemplate>
<asp:ImageButton ID="bt_Edit" runat="server" CommandName="Edit" AlternateText="Edit" ImageUrl="~/Images/Edit.gif" CssClass="ImageButton" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="bt_Update" runat="server" CommandName="Update" AlternateText="Update" ImageUrl="~/Images/save.png" CssClass="ImageButton" />
<asp:ImageButton ID="bt_Cancel" runat="server" CommandName="Cancel" AlternateText="Cancel" ImageUrl="~/Images/cancel.png" CssClass="ImageButton" />
</EditItemTemplate>
<HeaderStyle Width="60px" />
<ItemStyle Width="60px" Wrap="False" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<br />
<asp:ImageButton ID="Home" runat="server" ImageUrl="~/Images/home.png" CssClass="ImageButtonLarge" AlternateText="Home" ToolTip="Home" OnClick="Click_Home" />
</center>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
And the code behind page:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;
public partial class CountryGrid : System.Web.UI.Page
{
SnowballInterface conn = new SnowballInterface();
SnowballInterface CurSeason = new SnowballInterface();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_Init(object sender, EventArgs e)
{
if (Request["i_FK_CountryID"] == null)
{
Response.Redirect("Default.aspx");
}
#region Get Accom Data
SqlCommand EditCOComm = new SqlCommand("IFACE_JFA_COUNTRY", conn.sbConn);
EditCOComm.CommandType = CommandType.StoredProcedure;
EditCOComm.Parameters.Add("#Statement", SqlDbType.Char).Value = "CountryDetails";
EditCOComm.Parameters.Add("#i_FK_CountryID", SqlDbType.Int).Value = Request["i_FK_CountryID"].Trim().ToString();
try
{
conn.sbConn.Open();
SqlDataReader EditCOReader;
EditCOReader = EditCOComm.ExecuteReader();
// Download default values
while (EditCOReader.Read())
{
Country.Text = Convert.ToString(EditCOReader["Country_Name"]).Trim();
Season.Text = Convert.ToString(EditCOReader["Season_Name"]).Trim();
}
EditCOReader.Close();
}
catch (Exception)
{
}
finally
{
conn.sbConn.Close();
}
#endregion
if (!IsPostBack)
{
BindCountryGrid();
}
}
protected void CountryGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
CountryGridView.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindCountryGrid();
}
protected void CountryGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//Reset the edit index.
CountryGridView.EditIndex = -1;
//Bind data to the GridView control.
BindCountryGrid();
}
protected void CountryGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label Textbox_Accom = (Label)CountryGridView.Rows[e.RowIndex].FindControl("lbl_Accom_Code");
Label Textbox_Accom_Name = (Label)CountryGridView.Rows[e.RowIndex].FindControl("lbl_Accom_Name");
DropDownList Textbox_OP49 = (DropDownList)CountryGridView.Rows[e.RowIndex].FindControl("txt_OP49");
DropDownList Textbox_Weekly = (DropDownList)CountryGridView.Rows[e.RowIndex].FindControl("txt_Weekly");
DropDownList Textbox_Daily = (DropDownList)CountryGridView.Rows[e.RowIndex].FindControl("txt_Daily");
SqlCommand commEditConsultant = new SqlCommand("IFACE_JFA_ACCOM", conn.sbConn);
commEditConsultant.CommandType = CommandType.StoredProcedure;
commEditConsultant.Parameters.Add("#Statement", SqlDbType.VarChar).Value = "AccomGridUpdate";
commEditConsultant.Parameters.Add("#Page", SqlDbType.VarChar).Value = "OP49";
commEditConsultant.Parameters.Add("#PC_Username", SqlDbType.VarChar).Value = HttpContext.Current.User.Identity.Name.ToUpper().ToString();
commEditConsultant.Parameters.Add("#Season_Name", SqlDbType.VarChar).Value = Season.Text;
commEditConsultant.Parameters.Add("#Accom_Code", SqlDbType.VarChar).Value = Textbox_Accom.Text;
commEditConsultant.Parameters.Add("#i_FK_SeasonID", SqlDbType.VarChar).Value = Request["i_FK_SeasonID"].Trim().ToString();
commEditConsultant.Parameters.Add("#OP49_Required", SqlDbType.VarChar).Value = Textbox_OP49.Text;
commEditConsultant.Parameters.Add("#Weekly", SqlDbType.VarChar).Value = Textbox_Weekly.Text;
commEditConsultant.Parameters.Add("#Daily", SqlDbType.VarChar).Value = Textbox_Daily.Text;
conn.sbConn.Open();
commEditConsultant.ExecuteNonQuery();
CountryGridView.EditIndex = -1;
Error_Dashboard.Text = Textbox_Accom.Text + " - " + Textbox_Accom_Name.Text + " Updated Successfully!";
conn.sbConn.Close();
BindCountryGrid();
}
private void BindCountryGrid()
{
SqlCommand CountryGridSelect = new SqlCommand("IFACE_JFA_ACCOM", conn.sbConn);
CountryGridSelect.CommandType = CommandType.StoredProcedure;
CountryGridSelect.Parameters.Add("#Statement", SqlDbType.VarChar).Value = "CountryGridSelect";
CountryGridSelect.Parameters.Add("#i_FK_CountryID", SqlDbType.VarChar).Value = Request["i_FK_CountryID"].Trim().ToString();
try
{
conn.sbConn.Open();
SqlDataReader CountryGridSelectReader;
CountryGridSelectReader = CountryGridSelect.ExecuteReader();
CountryGridView.DataSource = CountryGridSelectReader;
CountryGridView.DataBind();
CountryGridSelectReader.Close();
}
finally
{
conn.sbConn.Close();
}
}
protected void Click_Home(object sender, EventArgs e)
{
Response.Redirect("Default.aspx?i_FK_SeasonID=" + Request["i_FK_SeasonID"].Trim().ToString());
}
}
You can set MaintanScrollPositionOnPostback to true in page directives on your aspx page.
<%# Page Language="C#" AutoEventWireup="true" MaintainScrollPositionOnPostback="true" CodeBehind="Gridview.aspx.cs" Inherits="TestWebsite.Gridview" %>

how to know the TOOL TIP of button clicked within the datalist

I need code example please.i tried selectedindexchange but it doesnot register any index change what to use?
its c# vs08 asp.net sql server
the code files are
.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{//not this
///Label3.Text = "clicked clicked clicked";
}
protected void Button1_Click1(object sender, EventArgs e)
{
Label5.Text = "the tool tip of the button clicked is! HELP!!!";
//here code please how to which button is clicked?
//there are many records so?
//even if i try to use the button id directly
//it does not appear
//to vs the button does not exist outside the datalist control
//help
}
}
the source file
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:test1 %>"
DeleteCommand="DELETE FROM [1] WHERE [ID] = #ID"
InsertCommand="INSERT INTO [1] ([ID], [NAME]) VALUES (#ID, #NAME)"
SelectCommand="SELECT * FROM [1]"
UpdateCommand="UPDATE [1] SET [NAME] = #NAME WHERE [ID] = #ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Decimal" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="NAME" Type="String" />
<asp:Parameter Name="ID" Type="Decimal" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ID" Type="Decimal" />
<asp:Parameter Name="NAME" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<br />
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
<br />
<asp:DataList ID="DataList2" runat="server" DataKeyField="ID"
DataSourceID="SqlDataSource3">
<ItemTemplate>
ID:
<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
<br />
NAME:
<asp:Label ID="NAMELabel" runat="server" Text='<%# Eval("NAME") %>' />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
-<asp:Label ID="Label2" runat="server" Text='<%# Eval("NAME") %>'></asp:Label>
<br />
<br />
<br />
<asp:Label ID="Label4" runat="server" Text='<%# Eval("ID") %>'
ToolTip='<%# Eval("NAME") %>'></asp:Label>
<br />
here extra information/ description is binded to tool tip.<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click1"
Text='<%# Eval("ID") %>' ToolTip='<%# Eval("NAME") %>' />
<br />
when clicked, the text of the button is displayed in the label. but many records
so button belonging to which record clicked?<br />
<br />
<br />
<hr />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<br />
EDIT
<asp:DataList ID="DataList2" runat="server" DataKeyField="ID"
DataSourceID="SqlDataSource3">
<ItemTemplate>
ID:
<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
<br />
NAME:
<asp:Label ID="NAMELabel" runat="server" Text='<%# Eval("NAME") %>' />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
-<asp:Label ID="Label2" runat="server" Text='<%# Eval("NAME") %>'></asp:Label>
<br />
<br />
<br />
<asp:Label ID="Label4" runat="server" Text='<%# Eval("ID") %>'
ToolTip='<%# Eval("NAME") %>'></asp:Label>
<br />
here extra information/ description is binded to tool tip.<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click1"
Text='<%# Eval("ID") %>' ToolTip='<%# Eval("NAME") %>' />
<br />
when clicked, the text of the button is displayed in the label. <br />
<br />
<br />
<asp:Button ID="Button2" runat="server" CommandArgument='<%# Eval("NAME") %>'
CommandName="Explain" Text='<%# Eval("ID") %>' />
<asp:TextBox ID="TextBox1" runat="server">First Record</asp:TextBox>
<br />
when clicked takes argument from button and the text in the text box, displayed.
(record 1)<br />
<br />
<br />
<br />
<asp:Button ID="Button3" runat="server" CommandArgument='<%# Eval("NAME") %>'
CommandName="Explain" Text='<%# Eval("ID") %>' />
//<br />
when clicked does the same as above
<br />
<hr />
<br />
<br />
</ItemTemplate>
</asp:DataList>
code behind
protected void DataList2_ItemCommand(object sender, DataListCommandEventArgs e)
{
// all of the buttons within the row that have the CommandName property set can cause this event handler to execute.
// Use the CommandName argument to determine which button was clicked and take the appropriate action
switch (e.CommandName)
{
case "Explain":
// update your label using the command argument rather that the button's ToolTip
Label5.Text = e.CommandArgument.ToString();
TextBox TextBox1 = e.Item.FindControl("TextBox1") as TextBox;
Label6.Text = TextBox1.Text;
break;
default:
Label5.Text="ERROR";
break;
}
}
mistake:- i forgot to put
OnItemCommand="MyDataList_ItemCommand"
in datalist source code
...
You could do this:
protected void Button1_Click1(object sender, EventArgs e)
{
Label5.Text = (sender as Button).ToolTip;
}
Also, if you know that you want to work with other controls within that row, you could use the DataList.ItemCommand event instead of the Button.Click event. Below is an example of how you might do that:
ASP Markup:
<asp:Label ID="MyLabel" runat="server" />
<asp:DataList ID="MyDataList" runat="server" OnItemCommand="MyDataList_ItemCommand">
<ItemTemplate>
<!-- Suppose you had some input controls that you needed to work with as well -->
<asp:TextBox ID="txtInput1" runat="server" />
<asp:TextBox ID="txtInput2" runat="server" />
<asp:Button ID="btnMyCommand" runat="server" CommandName="MyCommand" CommandArgument='<%# Eval("NAME") %>' Text='<%# "Execute My Command on ID:" + Eval("ID") %>' ToolTip='<%# string.Format("This will execute the \"My Command\" command on {0}.", Eval("NAME")) %>' />
<!-- just some examples of other buttons on the same row that execute different commands -->
<asp:Button ID="btnDoSomethingCrazy" runat="server" CommandName="Do Something Crazy!" Text="Do Something Crazy!" />
<asp:LinkButton ID="btnEdit" runat="server" CommandName="Edit" Text="Edit" />
</ItemTemplate>
</asp:DataList>
Code-Behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack && !Page.IsCallback)
{
// some example data
MyDataList.DataSource = new[] {
new { ID = 1, NAME = "ABCD" },
new { ID = 2, NAME = "BCDE" },
new { ID = 3, NAME = "CDEF" },
};
MyDataList.DataBind();
}
}
protected void MyDataList_ItemCommand(object sender, DataListCommandEventArgs e)
{
// all of the buttons within the row can cause this event handler to execute.
// Use the CommandName argument (populated by the CommandName property of the button that was clicked) in order to determine which button was clicked and take the appropriate action
switch (e.CommandName)
{
case "Edit":
// ...
break;
case "Update":
// ...
break;
case "Cancel":
// ...
break;
case "Delete":
// ...
break;
case "MyCommand":
// update your label using the command argument rather that the button's ToolTip
MyLabel.Text = e.CommandArgument.ToString();
TextBox txtInput1 = e.Item.FindControl("txtInput1") as TextBox;
TextBox txtInput2 = e.Item.FindControl("txtInput2") as TextBox;
string value1 = txtInput1.Text;
string value2 = txtInput2.Text;
// do something with the input values
break;
case "Do Something Crazy!":
// ...
break;
}
}
You can try casting the sender:
protected void Button1_Click1(object sender, EventArgs e)
{
Button myButton = (Button)sender;
Label5.Text = myButton.ToolTip;
}

Categories