I have a GridView on which I am binding 2 textbox and a update button. grid view is dynamic so Row numbers can vary depending upon the size of dataset. Now what I want is when I change values in this textbox and click on update then data for that row in database should get updated. The textbox values in database should get updated.
This is my code:
<asp:GridView ID="dgAbstractSummary" runat="server" AutoGenerateColumns="False" OnRowDataBound="dgAbstractSummary_RowDataBound" OnRowCreated="dgAbstractSummary_RowCreated"
Visible="true">
<Columns>
<asp:BoundField HeaderText="ID" DataField="id" HtmlEncode="false"></asp:BoundField>
<asp:BoundField HeaderText="Abstract Title" DataField="title" HtmlEncode="false"></asp:BoundField>
<asp:TemplateField HeaderText="Significance Score">
<ItemTemplate>
<asp:TextBox ID="txtSignificanceScore" AutoPostBack="true" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"significanceScore")%>'></asp:TextBox>
<div class="voffset3"></div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Innovation Score">
<ItemTemplate>
<asp:TextBox ID="txtInnovationScore" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"innovationScore")%>'></asp:TextBox>
<div class="voffset3"></div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button AutoPostBack="true" CssClass="btn btn-primary btn-embossed" ID="btnUpdate" runat="server"
Text="Update"
OnClick="btnUpdate_Click" CommandArgument='<%#Eval("Id") %>' />
<div class="voffset3"></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="panel-default"></HeaderStyle>
<RowStyle CssClass=""></RowStyle>
</asp:GridView>
C#
dgAbstractSummary.DataSource = dsResult; // dataset contains values which I am binding to grid columns
dgAbstractSummary.DataBind();
protected void btnUpdate_Click(object sender, EventArgs e)
{
Button myUpdateButton = (Button)sender;
}
Here Id (first column) is the unique key for each row.
Use find control function to find the two text boxes on the grid view row using your buttons sender event.
Something like this.
Button btn = (Button)sender;
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
TextBox details = gvr.FindControl("detailsText") as TextBox;
//do something with details
TextBox cusID = gvr.FindControl("TextBox2") as TextBox;
Related
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" CssClass="outdata">
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Center" ItemStyle-CssClass="profiledata" ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<input id="Checkbox2" type="checkbox" style="width: 50px !important;" onclick="CheckAll(this)" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" OnCheckedChanged="chkSelect_CheckedChanged" runat="server" />
<asp:HiddenField runat="server" ID="hdnAssid" Value='<%# Eval("Asset_ID") %>' />
</ItemTemplate>
<ItemStyle Width="5px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Stop" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlGrdStops"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Suppose I have 2 columns in Gridview.
1. CheckBox
2. DropDownList
What Should be the code in the OnCheckedChange event of checkbox that whenever i check it the dropdownlist of the same row should be filled.
I tried RowDataBound event of gridview but the data is too large,so its take a long time in processing and it unnecessarily checks every rows checkbox.
Plase help since I am new.
Thanks in advance. :)
You can use the NamingContainer of the CheckBox to locate the DropDownList in the same row. PS set AutoPostBack to true on the CheckBox.
protected void chkSelect_CheckedChanged(object sender, EventArgs e)
{
//cast the sender back to a checkbox
CheckBox cb = sender as CheckBox;
//get the current gridviewrow from the checkbox namingcontainer
GridViewRow row = cb.NamingContainer as GridViewRow;
//use findcontrol to locate the dropdownlist in that row
DropDownList ddl = row.FindControl("ddlGrdStops") as DropDownList;
//add the items to the dropdownlist
ddl.Items.Add(new ListItem() { Text = "DropDownList found", Value = "1" });
}
ASP.net C#
I am creating a gridview having 5 rows containing detail of operations users have to perform. Detail of operations should be filled when it is completed. At a time any number of operation details can be filled.
In gridview, first Item-template contains Label (for operation name) and others are textbox (for other details).
If any user has filled 3 rows then rest of two rows should be blank.
My problem is how to bind those 3 rows filled previously leaving two bottom rows available for entry.
My Gridview design is :
<asp:GridView CssClass="table-bordered gridStyle" runat="server" ShowFooter="True"
ID="grdOperationEntry" GridLines="None" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Operation">
<ItemTemplate>
<asp:Label Text='<%# Eval("operation_title") %>' ID="lblOperationName" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date of Completion">
<ItemTemplate>
<asp:TextBox runat="server" Text='<%# Eval("date_completed") %>' CssClass="form-control"
ID="txtDateCompletion" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time Taken">
<ItemTemplate>
<asp:TextBox runat="server" Text='<%# Eval("time_taken") %>' ID="txtTimeTaken" CssClass="form-control" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Score">
<ItemTemplate>
<asp:TextBox runat="server" Text='<%# Eval("score_gain") %>' ID="txtScore" CssClass="form-control" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reported To">
<ItemTemplate>
<asp:TextBox runat="server" Text='<%# Eval("reported_to") %>' ID="txtReportedTo"
CssClass="form-control" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
</asp:GridView>
Add a Label with all TextBoxes in GridView and set them visible false and bind also Label from database. I have added an example TemplateField to below, you can do it for all other:
.....
<asp:TemplateField HeaderText="Date of Completion">
<ItemTemplate>
<asp:Label runat="server" Visible="False" Text='<%# Eval("date_completed")
ID="lblDateCompletion" %>'></asp:Label>
<asp:TextBox runat="server" Visible="False" Text='<%# Eval("date_completed") %>'
ID="txtDateCompletion" />
</ItemTemplate>
</asp:TemplateField>
.....
In RowDataBound event set them visible true:
protected void grdOperationEntry_RowDataBound(object sender, GridViewRowEventArgs e)
{
// check if gridview row not a header or footer
if (e.Row.RowType == DataControlRowType.DataRow)
{
// get controls by id from gridview and cast them
Label lblDateCompletion = e.Row.FindControl("lblDateCompletion") as Label;
TextBox txtDateCompletion = e.Row.FindControl("txtDateCompletion") as TextBox;
if (lblDateCompletion.Text == null)
txtDateCompletion.Visible = true;
else
lblDateCompletion.Visible = true;
// perform same for other controls
}
}
Note: Don't forget to add OnRowDataBound property to your GridView <asp:GridView ID="grdOperationEntry" runat="server" OnRowDataBound="grdOperationEntry_RowDataBound" >
I have GridView and a button as follows. Then i am binding the gridview with data from my database. GridView has two hiddenfield for Id and ClassIndex.
when i selecting a checkbox and click button, i want to get the corresponding Id and FileName.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="check" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdfId" runat ="server" Value='<%#Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdfClssIndex" runat ="server" Value='<%#Eval("ClassIndex") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblFileName" runat ="server" Text='<%#Eval("FileName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and Button Like
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Send Request" />
the code behind button is
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
var check = row.FindControl("check") as CheckBox;
if (check.Checked)
{
int Id = Convert.ToInt32(row.Cells[1].Text);
//some logic follws here
}
}
}
but i am getting an error like
Input string was not in a correct format.
What is the error and how to solve it?
Your looping correct.
But you forgot to notice one thing here, when you wanted to access CheckBox you did a FindControl on row. Which means you are trying to find some control in that row.
Then why are you accessing HiddenField control inside row with row.Cell[1].Text?
Try to find that also.
int Id = Convert.ToInt32(((HiddenField)row.FindControl("hdfId")).Value);
This is a simple gridView I m using in aspx web page
<asp:GridView ID="Order" runat="server" AutoGenerateColumns="False" ShowFooter="True" GridLines="none"
ItemType="MyProject.Models.TempList" SelectMethod="GetList" >
<Columns>
<asp:BoundField DataField="ItemID" HeaderText="ID" SortExpression="ItemID" />
<asp:TemplateField HeaderText="ItemName">
<ItemTemplate>
<asp:Label runat="server" ID="ItemName" Width="40" Visible="true" text="<%#: Item.Item.ItemName %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price(each)">
<ItemTemplate>
<%#: String.Format("{0:c}", Convert.ToDouble(Item.Item.UnitPrice))%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<div style="float:left">
<asp:Button ID="DeleteBtn" runat="server" Text="Delete" OnClick="DeleteBtn_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have 2+ items in a list<> which is populating the gridView, how can I tell (in the codeBehind.cs) that which Row the "DeleteBtn" was clicked on?
I was using a for loop to iterate every item in gridView using Rows[i] and used a check box to know which item wants to be deleted using a Update button.
But I want to do it directly on a custom created deletebutton.
Thanks in advance, I know I'm missing something silly.
The best way to do that is using CommandName and CommandArgument in your button declaration like that
<asp:Button ID="AddButton" runat="server" CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Add to Cart" />
you can pass any value in the case we pass the rowIndex, you can get a propertie from your object like that CommandArgument="<%# Eval("id") %>" after that you will handle the onRowCommand method
protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here to add the item to the shopping cart.
}
}
hope it helps :)
Use the button's CommandArgument property; assign Container.DataItemIndex to it. Then use the OnRowCommand event of the gridview and grab the index.
Sample aspx:
<asp:Label runat="server" ID="lblMsg" />
<asp:GridView runat="server" id="gvSample" AutoGenerateColumns="false" OnRowCommand="PerformOperation">
<Columns>
<asp:BoundField DataField="RowValue"/>
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Delete" runat="server" CommandName="MyCustomCommand" CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateGrid();
}
}
private void PopulateGrid()
{
gvSample.DataSource = Enumerable.Range(0, 10).Select(i => new { RowValue = i });
gvSample.DataBind();
}
protected void PerformOperation(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "MyCustomCommand")
{
var rowIndex = int.Parse(e.CommandArgument);
lblMsg.Text = string.Format("Button on row index: {0} was clicked!", rowIndex);
}
}
Let the button field be given a CommandName, say for example a unique string myCommandName in the Edit columns dialog box. Don't worry about the CommandArgument. Then, in the Grid view Row command event, you can check which button (i.e. column) is clicked by tracing the command name like if e.commandname = "mycommandname", at the same time the CommandArgument will also be available as String and all we have to do is to converttoint32, something like intSelectedRow = convert.ToInt32(e.CommandArgument) which will give us the selected row's index.
I have two GridViews. GridView1 contains 2 link buttons in a column. When I select a link button in GridView1, the details of the link button should be displayed in GridView2.
GridView2 contains a column with radio buttons. I need to fill the radio buttons dynamically from the database.
How can I fill the Radio button list of GridView2 by using GridView1_RowCommand? Or Can I get it from the RowDataBound event of GridView2?
code behind:
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Yes")
{
GridViewRow gvRow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int RowIndex = gvRow.RowIndex;
Int32 iAppID = Convert.ToInt32(GridView1.DataKeys[gvRow.RowIndex].Value.ToString());
dset = userApps.UserSelectedApp(iUserID, iAppID);
if (dset.Tables[0].Rows.Count > 0)
{
GridViewRow gRow = GridView2.Rows[RowIndex];//I need to create object to this Gridview2, and fill the radiobutton list with some values
RadioButtonList rdbtnSubPlans = (RadioButtonList)e.gRow.Cells[2].FindControl("rdbSubPlans");
ds = userApps.UpgradePlans(iUserID, iAppID);
if (ds != null)
{
rdbtnSubPlans.DataSource = ds;
rdbtnSubPlans.DataValueField = "PlanID";
rdbtnSubPlans.DataTextField = "Plans";
rdbtnSubPlans.DataBind();
}
}
}
if (e.CommandName == "No")
{}
dtset = user.UserSelectedAppication(iUserID, iAppID);
GridView2.DataSource = dtset;
GridView2.DataBind();
MultiView1.SetActiveView(viewRenewOrUpdate);
}
}
ASPX code for the GridViews
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ApplicationID"
OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand"
OnSorting="GridView1_Sorting" OnDataBound="GridView1_DataBound"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="S.No" ItemStyle-HorizontalAlign="center" HeaderStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label ID="l1" runat="server" Text="1"></asp:Label>
</ItemTemplate>
<HeaderStyle Width="10%" />
</asp:TemplateField>
< Some Bound Fields>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="lnkYes" runat="server" Text="Yes"
CssClass="lnkbtn" Visible="false" commandname="Yes" Width="100px" >
</asp:LinkButton>
<asp:LinkButton ID="lnkNo" runat="server" CssClass="lnkbtn" Text="No"
Visible="false" commandname="No" ToolTip="No and Yes current plan" Width="100px" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="GridView2" runat="server" DataKeyNames="ApplicationID"
OnRowDataBound="GridView2_RowDataBound">
<Columns>
<asp:BoundField DataField="ApplicationName" HeaderText="Application name">
<HeaderStyle Width="30%" />
<ItemStyle CssClass="col" />
</asp:BoundField>
<asp:TemplateField HeaderText="Plans">
<ItemTemplate>
<asp:RadioButtonList ID="rdbSubPlans" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="rdbSubPlan_OnSelectedIndexChanged" Enabled="false">
</asp:RadioButtonList>
</ItemTemplate>
<ItemStyle CssClass="col" />
</Columns>
</asp:GridView>
Fill Gridview2 first, and then do the following corrections:
GridViewRow gRow = GridView2.Rows[0]
RadioButtonList rdbtnPlans = (RadioButtonList)gRow.FindControl("rdbPlans");