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
Related
I have a gridview with few controls in it and I'm trying to get these controls in OnRowCommand event by using the FindControl method, but it always returns null.
This is the gridview
<asp:GridView ID="GridView1" runat="server" CssClass="table table-striped table-bordered" AutoGenerateColumns="false" DataKeyNames="ID" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"
OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Service Type">
<ItemTemplate>
<asp:Label Text='<%# Eval("Fund_Service_Type") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlServiceType" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Method">
<ItemTemplate>
<asp:Label Text='<%# Eval("Fund_Method") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlFundMethod" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Source">
<ItemTemplate>
<asp:Label Text='<%# Eval("Fund_Source") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlFundSource" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:Label Text='<%# Eval("Fund_Amount") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAmount" runat="server" Text='<%# Eval("Fund_Amount")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" HeaderText="" ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="btnRedirect" runat="server" CommandArgument='<%# Bind("ID") %>' CommandName="CompleteTransaction" Text="Complete Transaction"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And here is the row command event
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("CompleteTransaction"))
{
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int rowIndex = Convert.ToInt32(e.CommandArgument); // Get the current row
DropDownList ddlServiceType = (DropDownList)GridView1.Rows[rowIndex].Cells[0].FindControl("ddlServiceType");//retuns null
GridViewRow selectedRow = GridView1.Rows[rowIndex];
DropDownList name = (DropDownList)gvr.Cells[0].FindControl("ddlServiceType"); //also returns null
Server.Transfer("~/Transaction.aspx");
}
}
what I'm trying to do is get the selected row so and values from its controls, so that I can use them in Transaction page.
Edit: The FindControl method is working fine in RowUpdating and RowDataBound events
Since your DropDownLists is in the EditItemTemplate, you need to use the EditIndex to get the correct row.
DropDownList ddlServiceType = (DropDownList)GridView1.Rows[GridView1.EditIndex].FindControl("ddlServiceType");
ddlServiceType.BackColor = Color.Red;
this may help you out
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("CompleteTransaction"))
{
int rowIndex = Convert.ToInt32(e.CommandArgument); // Get the current row
GridViewRow row = GridView1.Rows[index];
Label lblName = (Label)row.FindControl("lblName")
DropDownList drpList= (DropDownList)row.FindControl("ddlServiceType");
lblName.Text = drpList.SelectedValue;
}
}
I have a Gridview and inside I have another one nested GridView. When I press a plus button the nested GridView expands using a JavScript. The nested GridView expands on edit mode using TextBox controls. So when the user types on a TextBox would have the ability to update the cell using an update button. My problem is that when I press the update button the update occurs but not how I would expected. If for example the initial value of a cell was “My name is Peter” and I have done the edit “I don’t have a name” The new value that will be saved is exactly this: “My name is Peter, I don’t have a name”. The databind of the nested GridView occurs on the parent GridView DataBound event.
My code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="gridView_PageIndexChanging"
AutoGenerateColumns="False" DataKeyNames="myitemID"
OnRowDataBound="GridView_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="../plus.png" />
<asp:GridView ID="nestedGridView" runat="server"
AutoGenerateColumns="False"
DataKeyNames="mynestedID">
<Columns>
<asp:TemplateField HeaderText="nestedID" Visible="false" ItemStyle-Width="20%"
SortExpression="nesteditemID">
<ItemTemplate>
<asp:Label ID="nesteditemID" runat="server" Text='<%# Bind("nesteditemID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" ItemStyle-Width="20%"
SortExpression="Name">
<ItemTemplate>
<asp:TextBox ID="name" TextMode="MultiLine" Width="80%" Rows="3" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:Panel ID="mypanel" runat="server">
<table>
<tr>
<td>
<asp:ImageButton ID="ImageButton2" OnClick="updatename_Click" ImageUrl="~/images/update.jpg" Width="15px" Height="15px" runat="server"></asp:ImageButton>
</td>
</tr>
</table>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="myitemID" InsertVisible="False"
SortExpression="myitemID" Visible="False">
<ItemTemplate>
<asp:Label ID="myitemID" runat="server" Text='<%# Bind("myitemID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ItemName" ItemStyle-Width="20%"
SortExpression="ItemName">
<ItemTemplate>
<asp:Label ID="ItemName" runat="server" Text='<%# Bind("ItemName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
cs code:
protected void updatename_Click(object sender, EventArgs e)
{
GridViewRow masterrow = (GridViewRow)(sender as Control).Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent;
GridViewRow row = (GridViewRow)(sender as Control).Parent.Parent.Parent;
int index = row.RowIndex;
int mi = masterrow.RowIndex;
int i = index;
GridView nestedGridView = (GridView)GridView1.Rows[mi].FindControl("nestedGridView");
Label nestedID = (Label)nestedGridView.Rows[index].FindControl("nestedID");
int sbid = Convert.ToInt32(nestedID.Text);
TextBox name = (TextBox)nestedGridView.Rows[index].FindControl("name");
string myname = Convert.ToString(name.Text);
//update name with the new value
Nesteditem updatenesteditem = mylinqobjects.Nesteditems.Single(p => p.nesteditemID == sbid);
if (!string.IsNullOrEmpty(myname))
{
updatenesteditem.nesteditemName = myname;
mylinqobjects.SubmitChanges();
}
}
Replaced the current text by removing the old one.
string myname = name.Text.Substring(name.Text.LastIndexOf(",")+1);
Tried all possiblities, but due nested grid view rendering and its restrictions, we could do only like above.
Any others solutions, please provide.
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>
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");
....
}
I have a ASP:grid which has a link button, i need to some how reference that on the code behind when its clicked but im struggling on the syntax
Heres my ASP:Grid i need to execute code in the code behind when that link button 'Re-Take' is pressed and also be able to know what row it was clicked on as i will need to reference the users emails and name and then send an email with the relevant information....
<asp:GridView ID="GrdViewUsers" runat="server" AutoGenerateColumns="false" GridLines="None"
EnableViewState="false" class="tablesorter">
<AlternatingRowStyle></AlternatingRowStyle>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Re-Take" runat="server" ID="Edit" CommandName="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Full Name">
<ItemTemplate>
<asp:HyperLink ID="HyperFullName" CssClass="gvItem" runat="server" NavigateUrl='<%#Eval("UserID","/ExamPaper.aspx?uid={0}") %>'
Text='<%# DataBinder.Eval(Container,"DataItem.FullName") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="lblSurname" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exam Taken">
<ItemTemplate>
<asp:Label ID="lblUsername" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ExamTaken") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Taken">
<ItemTemplate>
<asp:Label ID="lblUsername" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DateTaken") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exam Total">
<ItemTemplate>
<asp:Label ID="lblUsername" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ExamTotal") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
If someone can help me with a snippet i would highly appreciate it
You could approach this slightly different. You see, when a control is placed inside a gridview, any event raised from that control raises also the RowCommand on the GridView.
To get what you want you could then add both CommandName and CommandArgument to your LinkButton and then catch it in the GridView's RowCommand.
<asp:LinkButton id="LinkButton1" runat="server" commandName="LinkButtonClicked" commandArgument='Eval("myObjectID")' />
where myObjectID is the name of the ID column of your object you bind the grid to.
Then
void GridView1_RowCommand( object sender, GridViewCommandEventArgs e )
{
if ( e.CommandName == "LinkButtonClicked" )
{
string id = e.CommandArgument; // this is the ID of the clicked item
}
}
see: ASP.net GridView: get LinkItem's row
FindControl should work in this case.
protected void GrdViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink myHyperLink = e.Row.FindControl("Edit") as HyperLink;
}
}
First: You have repeating ID's in your TemplateFields like lblUsername what is not allowed since it's the same NamingContainer.
You can pass the RowIndex as CommandArgument to RowCommand:
on aspx:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton
Text="Re-Take"
runat="server"
ID="Edit"
CommandName="Edit"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Handle the GridView's RowCommand:
void GrdViewUsers_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Edit")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GrdViewUsers.Rows[index];
// now you can get all of your controls like:
Label lblSurname = (Label)row.FindControl("lblSurname");
String email = lblSurname.Text // you noticed that DataItem.Email is bound to lblSurname?
}
}
Suppose the grid has linkbutton on which we want to get row index
<asp:LinkButton ID="lnkbtnAdd " runat="server" CommandName="cmdAdd" ImageUrl="~/Images/add.gif" ></asp:LinkButton>
In code behind, In OnRowCreated event we attach row number of grid to each button of row to get it back when it is clicked in RowCommand event
protected void gvListing_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Web.UI.WebControls.LinkButton lnkbtnAdd = new System.Web.UI.WebControls.LinkButton();
lnkbtnAdd = (System.Web.UI.WebControls.LinkButton)e.Row.FindControl("lnkbtnAdd");
if (lnkbtnAdd != null)
lnkbtnAdd .CommandArgument = e.Row.RowIndex.ToString();
}
}
In RowCommand event we will get back the current row index and set the selected index of the grid
protected void gvListing_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToString() == "cmdAdd")
{
int RowIndex = int.Parse(e.CommandArgument.ToString());// Current row
}
}