Fire SelectionIndexChanged with AutoGenerateSelect "False" - c#

I have a datagrid view, and I'd like to capture a datakeyname value on a selected row of a gridview. But in this scenario one of my columns (Event Title) will be a hyperlink, and when this link is clicked it will fire selectedindexchanged and capture DataKeyName which will be passed to populate a form. Here's my code. Currently my hyperlink field is not eve "clickable". Do I need to change my approach?
<asp:GridView ID="gvEventDetails" CssClass="gvEvent" runat="server" DataKeyNames="Event_ID"
AutoGenerateColumns="false" OnSelectedIndexChanged="gvEventDetails_SelectedIndexChanged" >
<Columns>
<asp:TemplateField HeaderText="Event Title">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" Text='<%# Eval("Event_Title") %>' runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Client Name">
<ItemTemplate>
<asp:Label ID="ClientName" runat="server" Text='<%# Eval("Name") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
The method:
protected void gvEventDetails_SelectedIndexChanged(object sender, EventArgs e)
{
string DataKey = gvEventDetails.SelectedValue.ToString();
}

Instead of a hyperlink, try a linkbutton with command name:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="Select" CommandArgument='<%# Eval("Event_ID") %>' Text='<%# Eval("Event_Title") %>'
...
The rule is that posting back with common command names (Edit, Select, Delete, Cancel etc.) is correctly recognized by the event engine.

This is what I ended up doing
protected void gvEventDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
string PK = (e.CommandArgument).ToString();
}
<asp:TemplateField HeaderText="Event Title">
<ItemTemplate>
<asp:LinkButton ID="HyperLink1" runat="server" Text='<%# Eval("Event_Title") %>'
CommandArgument='<%# Eval("Event_ID") %>'
></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Related

How Do I Transfer Grid view values to next page

How Do I Transfer Grid view values to next page. Grid view consist one of the text box(txtItemGroup) which values have to be entered by the user dynamically, not from the Data base.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" HorizontalAlign="Center">
<Columns>
<asp:TemplateField HeaderText="Item Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("TestItemName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Items Group">
<ItemTemplate>
<asp:Label ID="lblGroup" runat="server" Text='<%# Eval("TestItemGroup") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group">
<ItemTemplate>
<asp:TextBox ID="txtItemGroup" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Values">
<ItemTemplate>
<asp:Label ID="lblItemValue" runat="server" Text='<%# Eval("TestItemValues") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Default Values">
<ItemTemplate>
<asp:Label ID="lblDefaultValues" runat="server" Text='<%# Eval("DefaultValues") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In your GridView's RowCommand event put in the following code:
if (e.CommandName == "Send")
{
TextBox txt = (TextBox)GridView1.FindControls("txtItemGroup");
Session["ItemGroup"] = txt.Text;
}
In addition to this, put a Button or LinkButton in your GridView's ItemTemplate to send the value of the appropriate Textbox as you have put a TextBox and set it's CommandName property in the .aspx page as:
<asp:Button id="btnSend" runat="server" Text = ">" commandName="Send"/>
And then in the Page_Load event of the page where you want the value of TextBox to be viewed:
if (!IsPostback)
{
Label1.Text = Session["ItemGroup"].ToString();
//or
Label1.Text = (String)Session["ItemGroup"];
}
try this:-
Add a button just below that textbox like this:-
<asp:Button ID="btnsend" Text="Send" runat="server" CommandName="Send" />
In RowCommand event write like this:-
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Send")
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
TextBox TextBox1 = row.FindControl("txtItemGroup") as TextBox;
Response.Redirect("yourasppage.aspx?txt=" + TextBox1.Text);
}
}
On page load of `yourasppage.aspx' use this code
string txtval = Request.QueryString["txt"];
Hope this will help

Insert new line, not getting text from textboxes

I'm having problems with a web app. I have a gridview connected with SQL datatable and in the footer I have textboxes (or dropdownlists) and an "insert" button. The problem occurs when I click on the button, and want to insert new line in my SQL table. It does not read text (we need to put in some strings) from textboxes. Of course insert does not happen, since some of the data must not be null.
I have the same problem with Editing rows.
this is my code:
Gridview:
<asp:GridView ID="gvUser" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" OnRowDataBound="gvUser_RowDataBound"
OnRowCommand="gvUser_RowCommand" ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last name">
<ItemTemplate>
<asp:Label ID="lblLname" runat="server" Text='<%# Bind("lname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLname" runat="server" Text='<%# Bind("lname") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewLname" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<FooterTemplate>
<asp:LinkButton ID="lnkAdd" runat="server" CausesValidation="False" CommandName="Insert"
Text="Shrani"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
protected void BindGV()
{
//bind the SQL table to the GridView (no problem here)
}
protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
{
//here I bind the dropdownlist (did not include it in code snippet,
//since firstly I need to get text from textboxes
}
protected void gvUser_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert"))
{
TextBox txtNewName = (TextBox)gvUser.FooterRow.FindControl("txtNewName");
TextBox txtNewLname = (TextBox)gvUser.FooterRow.FindControl("txtNewLname");
string NewName = txtNewName.Text; //this strings come up empty (just "")
string NewLname = txtNewLname.Text; //it should read from textboxes
AddRow(NewName, NewLname);
BindGV();
}
}
private void AddRow(string name, string lname)
{
//insert row into SQL datatable
}
EDIT:
Well it works now. Found a simmilar problem, and the author said he was able to get it work with adding EnableViewState="false" to the Gridview.
I tried and it worked. :)
Anyone here able to answer why would this work? And how will this correspond with other gv functions?
Try with this code - based on e.Item.FindControl
if (e.CommandName.Equals("Insert"))
{
TextBox txtNewName = (TextBox)e.Item.FindControl("txtNewName");
TextBox txtNewLame = (TextBox)e.Item.FindControl("txtNewLname");
....
}

Updating GridView Row from Code Behind

When the user changes the text in the textbox's in the edit template and clicks update, when I try to grab those new values it still is graving the old value of the text box.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="CompanyID" CellPadding="4"
GridLines="None" Width="1079px" ForeColor="#333333"
OnRowCancelingEdit="GridView1_RowCancelling"
OnRowUpdating="GridView1_RowUpdating"
OnRowEditing="GridView1_RowEditing">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" CommandArgument='<%# Eval("CompanyID") %>' Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Issue Date">
<ItemTemplate>
<asp:Label runat="server" ID="IssueDate" Text='<%#Eval("IssueDate") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtIssueDate" Text='<%#Eval("IssueDate") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notice Intent Response Due">
<ItemTemplate>
<asp:Label runat="server" ID="NoticeIntentResponseDue" Text='<%#Eval("NoticeIntentResponseDue") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtNoticeIntentResponseDue" Text='<%#Eval("NoticeIntentResponseDue") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Deadline For Questions">
<ItemTemplate>
<asp:Label runat="server" ID="DeadlineForQuestions" Text='<%#Eval("DeadlineForQuestions") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtDeadlineForQuestions" Text='<%#Eval("DeadlineForQuestions") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bids Due">
<ItemTemplate>
<asp:Label runat="server" ID="BidsDue" Text='<%#Eval("BidsDue") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtBidsDue" Text='<%#Eval("BidsDue") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shortlist Notice">
<ItemTemplate>
<asp:Label runat="server" ID="ShortlistNotice" Text='<%#Eval("ShortlistNotice") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtShortlistNotice" Text='<%#Eval("ShortlistNotice") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Final Selection">
<ItemTemplate>
<asp:Label runat="server" ID="FinalSelection" Text='<%#Eval("FinalSelection") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtFinalSelection" Text='<%#Eval("FinalSelection") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false" HeaderText="CompanyID">
<ItemTemplate>
<asp:Label runat="server" ID="CompanyID" Text='<%#Eval("CompanyID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The update button calls this function:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
Label CompanyID = (Label)GridView1.Rows[e.RowIndex].FindControl("txtCompanyID");
TextBox thisIssueDate = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtIssueDate"));
TextBox NoticeIntentResponseDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNoticeIntentResponseDue");
Response.Write(NoticeIntentResponseDue.Text + " " + thisIssueDate.Text);
Response.End();
TextBox DeadlineForQuestions = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDeadlineForQuestions");
TextBox BidsDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBidsDue");
TextBox ShortlistNotice = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtShortlistNotice");
TextBox FinalSelection = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFinalSelection");
}
The response is showing me that the value being grabbed is still the origonal text value of the box. Not what you typed into the box.
In your grid view row updating event add the following condition
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit )
{
int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
Label CompanyID = (Label)GridView1.Rows[e.RowIndex].FindControl("txtCompanyID");
TextBox thisIssueDate = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtIssueDate"));
TextBox NoticeIntentResponseDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNoticeIntentResponseDue");
Response.Write(NoticeIntentResponseDue.Text + " " + thisIssueDate.Text);
Response.End();
TextBox DeadlineForQuestions = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDeadlineForQuestions");
TextBox BidsDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBidsDue");
TextBox ShortlistNotice = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtShortlistNotice");
TextBox FinalSelection = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFinalSelection");
}
}
Update:
the problem looks like that you have also bind your Edit Item template columns with the data from data table, and when you are getting the data in the code behind you are not getting the updated data which the user updates in edit mode and u still getting the old data. If you remove the Binding from the Edit Item Template feilds then your code will work.
I figured it out, Derek was right. It had to do with the Binding Data on postback in page load. I binded the data every time instead of just the first time. Thanks

jquery datepicker does not set date in ASP.net gridview EditItemTemplate and FooterTemplate's textBox

I am trying to implement a VERY simple ASP.net page: A GridView that allows user to edit old entry or insert a new entry. One of the columns in the GridView is holiday, so I use Jquery's datepicker to allow user to select a date.
Here is the ASP.net code
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:GridView ID="GridView1" runat="server" Height="300px"
ShowFooter="True"
AutoGenerateColumns="false"
OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowCommand="GridView1_RowCommand" >
<Columns>
<asp:TemplateField HeaderText="Date">
<EditItemTemplate>
<asp:TextBox ID="txtEditDate" runat="server" Text='<%# GetDate(Eval("CAL_DT")) %>' OnLoad="DisplayDatePicker1" ReadOnly="true"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewDate" runat="server" OnLoad="DisplayDatePicker2" ReadOnly="true"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%# GetDate(Eval("CAL_DT")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is my DisplayDatePicker1 method, DisplayDatePicker2 would be very similar.
protected void DisplayDatePicker1(object sender, EventArgs e)
{
StringBuilder scriptText = new StringBuilder();
string clientID = (sender as TextBox).ClientID;
scriptText.Append("$(function() {");
scriptText.Append("var DateSelector1 = $('#" + clientID + "'); ");
scriptText.Append("DateSelector1.datepicker();");
scriptText.Append(" });");
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"DateScript1", scriptText.ToString(), true);
}
So far so good. User can see the popup Datepicker and pick a date when he/she clicks Edit in the GridView row. Howerver, The following is the problem
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtEditHoliday = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditHoliday");
TextBox txtEditDate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditDate");
DropDownList dpdCountry = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("dpdCountry");
//txtEditDate.Text is not set,
}
txtEditDate.Text still contains the old date. The value selected by user via JQuery datepicker is not being passed to the txtEditDate.Text Please help! I spent many hours to search the internet but have not found any solutions.
To get the new, revised values - use :
e.NewValues
(from http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewupdateeventargs.aspx)

How do I get the Data from the textboxes within the gridview, nested in the repeater?

I tried posting this before with only text...there was only one response the suggested I update onblur..which didn't seem like the best route as there could be a considerable amount of records: I have the following Gridview built during a repeater ItemDataBound event:
'>
<asp:GridView ID="gvBeneficiary" DataKeyNames="BeneficiaryKey" OnRowCommand="gvBeneficiary_RowCommand" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink runat = "server" ID="hlEditBeneficiary" NavigateUrl='<%# "~/EditBeneficiary.aspx?bk=" + HttpUtility.UrlEncode(DataBinder.Eval(Container.DataItem, "BeneficiaryKey").ToString()) %>' Text='<%#Eval("FirstName") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Relationship" HeaderText="Relationship" />
<asp:TemplateField HeaderText="Shares %">
<ItemTemplate>
<asp:TextBox ID="txtEditShares" runat="server" Text='<%#Bind("Shares") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Beneficiary Type">
<ItemTemplate>
<asp:DropDownList ID="ddlBeneficiaryType" runat="server" SelectedIndex='<%# GetSelectedBeneficiaryType(Eval("BeneficiaryType")) %>' DataSource='<%# BeneficiaryTypes %>' ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Delete" CommandName='DoDelete' CommandArgument='<%# string.Format("{0}|{1}", Eval("BeneficiaryKey"), Eval("PlanTypeKey")) %>' ID="lbDoDelete" runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HyperLink ID="hlAddBeneficiary" runat="server" Text="Add Beneficiary" NavigateUrl='<%# "~/AddEditBeneficiary.aspx?PT=" + HttpUtility.UrlEncode(DataBinder.Eval(Container.DataItem, "PlanTypeKey").ToString()) + "&CPT=" + HttpUtility.UrlEncode(DataBinder.Eval(Container.DataItem, "CorePlanType").ToString()) %>'></asp:HyperLink>
This is the codebehind:
protected void rptPlanTypes_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
GridView gvBeneficiary = (GridView) e.Item.FindControl("gvBeneficiary");
gvBeneficiary.DataSource = ((PlanType) e.Item.DataItem).BeneficiaryCollection;
gvBeneficiary.DataBind();
}
I Have one onclick event from a button that I want to do all the updates from. Anyone have any idea how to drill down the the individual controls on the rows to pull the all the data at once or in a loop.
Thanks again for all of your suggestions.
Padawan
You will want to iterate over each row of the GridView and collect the data from each control in which you are interested. For example, the following snippet will grab each txtEditShares value per row.
foreach (GridViewRow row in gvBeneficiary.Rows)
{
var txtEditShares = (TextBox)row.FindControl("txtEditShares");
var shares = txtEditShares.Text;
///...
}
You'll need to put together the guts of the loop, but that's how move through each row of a GridView and grab control values. I hope it helps.

Categories

Resources