I have a gridview which gets populated with the help of a sqldatasource.
Once the gridview is created I want to add a manual column created Notes so that the user can enter notes for each row and then click the submit the form.
In the <Columns> field of the grid view I have this:
<asp:TemplateField HeaderText="Notes">
<ItemTemplate>
<asp:TextBox ID="txtNotes" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
This actually creates the new column just fine but when I enter data in the textboxes it disappears when clicking submit.
In the submit even I iterate through each gridview row like so:
foreach (GridViewRow row in gvResults.Rows)
{
row.Cells[0].Text =
...
string notes = (row.Cells[10].FindControl("txtNotes") as TextBox).Text;
}
So the string notes is always empty, it seems that on postback the value is reset; because it recreates the txtNotes textbox.
How can I keep the value on postback?
When you bind the GridView, make sure you check to see if it's a postback.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
LoadData(); //if you did this without checking IsPostBack, you'd rebind the GridView and lose the existing rows
}
protected void LoadData()
{
//bind GridView etc
}
Related
I would like to parse a row of data from Gridview in ASP.NET to a second page displaying contents of the row data from the previous page. My Gridview has already been linked to the database.
My current Gridview looks something like this:
I would like to achieve this when I click on the send details hyperlink:
If I click on the second row the data from that row will display in the next page
The following codes are what I had put under my link button itemTemplate:
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
<asp:LinkButton ID="viewTours" runat="server" CommandName="view" CommandArgument='<%# Bind("name") %>' PostBackUrl='<%#"~/details.aspx?RowIndex=" + Container.DataItemIndex %>'>View</asp:LinkButton>
</ItemTemplate>
This is my page load method from the second page where I want to load the data from.
protected void Page_Load(object sender, EventArgs e)
{
if (this.Page.PreviousPage != null)
{
int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
GridView GridView = (GridView)this.Page.PreviousPage.FindControl("GridView");
GridViewRow row = GridView.Rows[rowIndex];
name.Text = (row.FindControl("name") as Label).Text;
//name.Text = row.Cells[0].Text; (this did not work either, i got the same error)
}
}
However I get a squiggly line on name.Text saying that it does not exist, even though I do have a label with the id name in the design view of my html (second) page.
How can I make it such that I can parse data from the selected Gridview row to another page? Assuming that I can customize the second page and put the labels wherever I like.
This is the code from my gridview. As my binding has been done through the UI I dont have much codes for it, except to redirect the selected gridview row to another page.
protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
{
Session["tour"] = GridView;
Response.Redirect("tourDetails.aspx");
}
I still get the error that the label text does not exist, when it actually exists in the page itself.
Image of the Label with the id=name:
My label DOES contain runat="server":
You are confusing binding data with structure. When you try to get a control with the name key, you really mean Label1, so change
name.Text = (row.FindControl("name") as Label).Text;
to
name.Text = (row.FindControl("Label1") as Label).Text;
So I have a GridView for a C# web application, that has a Buttonfield, and when that button is clicked, I need to get the value of one of the fields for that row and store it in a variable for processing in some way.
However, neither the GridView nor the ButtonField seem to possess any means of doing this.
Can anyone recommend a way of getting data from a GridView, or if this is not possible, a different type of view that does offer this functionality, while still displaying a whole table (eg, not a DetailsView)
You can Check this link: https://msdn.microsoft.com/en-us/library/bb907626(v=vs.140).aspx.
Define the CommandName of the Button.
In the GridView Define the RowCommand Event and Check the CommandName.
Get the Index of the Row.
Get the Column with GridView.Rows[index](columnIndex)
If you are using asp:TemplateField like shown below then you can access the row content using RowCommand
Markup
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# Eval("CustomerID") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="AddCode" Text="Add New"/>
Code
protected void gvwSearch_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "AddCode")
{
var clickedButton = e.CommandSource as Button;
var clickedRow = clickedButton.NamingContainer as GridViewRow;
var rows_lblCode = clickedRow.FindControl("lblCode") as Label;
// now you can acccess all the label properties. For example,
var temp = rows_lblCode.Text;
}
}
I have a gridview with the following columns:
NAME|AGE|Birthday|Code
Joh 21 12.12.2 Yes/No
Currently the column code is a textbox. How can I transform it into a dropdownlist with 2 values: Yes/No so if I press edit I can choose in that cell the Value Yes or No.
Also How can I check on edit event too see if value is yes?
You can create a template field like this:
<columns>
<asp:TemplateField HeaderText="code">
<ItemTemplate>
<asp:DropDownList ID ="ddlCode" runat="server" AppendDataBoundItems="true" CssClass="DropDn1" />
</ItemTemplate>
</asp:TemplateField>
</columns>
In your rowdatabound event of the grid you will bind it if you want to bind from the database.
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList code= (DropDownList)e.Row.FindControl("ddlCode") as DropDownList;
if (code!= null)
{
//Bind the dropdownlist
}
}
To retrieve the value from the dropdown in your edit event you will do this:
string code = (row.FindControl("ddlCode") as DropDownList).Text);
I have two GridView in ASP.net 3.5 page. I have HyperLink field as one of the fields in the First GridView.
On click of this hyperlink I need to call display the 2nd grid by passing some values to a method showAllRecords(value from hyperlink)
How do I do this?
Thanks
You can try a TemplateField like this for GridView1 (primary GridView)
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="LinkButton1" CommandName="cmdName" CommandArgument='<%# Eval("IdColumn") %>' > LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
and in GridView1's RowCommand, you can get the CommandArgument and setup the DataSource for GridView2 (child GridView).
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName = "cmdName")
{
var arg = e.CommandArgument;
// use arg to filter GridView2's DataSource
GridView2.DataSource = FilteredDataSource;
GridView2.DataBind();
// show GridView2 if it's hidden.
}
}
Maybe the following blog post could give you a hint;
http://www.tugberkugurlu.com/archive/parent-child-view-in-a-single-table-with-gridview-control-on-asp-net-web-forms
First you need to handle SelectedIndexChanged event on the first grid and then get the value from the hyperlink. Is the hyperlink a DataKey? If it is then you get it by GridOne.SelectedDataKey.Values["key"] otherwise get the actual cell by valuefromGridOne = GridOne.SelectedRow.Cells[num].Text where number is the cell number. Once you have it you can pass the value to your second grid by handling Selecting event of the objectDataSource (assuming you use it to bind data) and passing the value like this e.InputParameters["dataKey"] = valuefromGridOne;
I have a FormView which contains 5 dynamic DropDownLists. By dynamic, I mean the lists of dropdowns can be increased or decreased - depending on how many questions I have in my DB.
When I click the save button, I'm able to retrieve and store the content of the text boxes. But I'm not able to retrieve the DropDownLists.
One of the problems is that the ID of the DropDownLists changes name after I click the Save Button.
Why? Because upon Databinding of my DropDownLists, I rename the ID's so that I can identify which is which and store the appropriate data in the DB.
I'm not sure if I'm "attacking" this all wrong.
Here's my aspx code:
<asp:FormView runat="server" (...)>
//Lots of textfields here
<asp:Repeater runat="server" id="myRepeater"> //Datasource is bound in codebehind.
<ItemTemplate>
<li>
<div class="value"> <asp:DropDownList id="score" runat="server" OnDataBinding="hotelCriterias_DataBinding" /> </div>
</li>
</ItemTemplate>
</asp:Repeater>
</asp:FormView>
Here I rename the ID's and databind the datasource
protected void myDropDownList_DataBinding(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)(sender);
ddl.ID = "question" + Eval("questionID").ToString(); //Rename ID to 'question1, question2...)
Repeater rep = (Repeater)myFormView.FindControl("myRepeater");
rep.DataSource = this.dtQuestions;
rep.DataBind();
}
Try using an ObservableCollection object class as an intermediary between your drop down list and DB access.
By setting the ObservableCollection based class to be the data context for the list and handling a DataContextChanged event, you will always know what data is being displayed without having to worry about keeping track of where it is displayed.
When you hit save button, you need to iterate your repeater, should be like
protected void btnSave_Click(object sender, EventArgs e)
{
if (Repeater1.Items.Count > 0)
{
for (int count = 0; count < Repeater1.Items.Count; count++)
{
DropDownList ddl = (DropDownList)Repeater1.Items[count].FindControl("ddl");
ddl.SelectedValue//
}
}
}