I have a Grid in which I am showing some records in each row. Here's how it is:
Now, my problem is that when I press the view button, I want to fetch the ID from the first column in a session variable so that I can display the same ID on the next page.
For the ItemTemplate of EditButton, I am using this code:
<ItemTemplate>
<asp:LinkButton ID="EditBtn" CssClass="btn green" CommandName="edit" ToolTip="Edit" Text="Edit" runat="server" />
</ItemTemplate>
You could try passing it as command argument:
<ItemTemplate>
<asp:LinkButton
ID="EditBtn"
CssClass="btn green"
CommandName="edit"
CommandArgument='<%# Eval("FirstColumnId") %>'
OnCommand="EditCommand"
ToolTip="Edit"
Text="Edit"
runat="server" />
</ItemTemplate>
and in the code behind:
protected void EditCommand(object sender, GridViewCommandEventArgs e)
{
var id = e.CommandArgument;
// TODO: do something with the id
}
Try this. No changes are required at your aspx
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Session["UserID"] = ((Label)GridView1.Rows[e.NewEditIndex].FindControl("lb1")).Text.Trim();
}
Other method (Using DataKeyNames - Preferred)
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Session["UserID"] = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
}
Related
I have a GridView which contains a Link Button inside a Template Field. The code is shown below:
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" onrowediting="gv1_RowEditing"
onrowcommand="gv1_RowCommand">
<Columns>
<asp:BoundField DataField="inDetailsId" HeaderText="inDetailsId"
SortExpression="inDetailsId" />
<asp:BoundField DataField="inUserId" HeaderText="inUserId"
SortExpression="inUserId" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnk1" runat="server" Text='<%# Eval("attDate")%>' CommandName="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="attstatus" HeaderText="attstatus"
SortExpression="attstatus" />
<asp:BoundField DataField="inAttendanceStatusId"
HeaderText="inAttendanceStatusId" SortExpression="inAttendanceStatusId" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:LearnConnectionString %>"
SelectCommand="SELECT * FROM [attendance]"></asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Button" />
The code-behind is below:
protected void gv1_RowCommand(object sender, GridViewCommandEventArgs e)
{
gv1.EditIndex = 1;
}
On clicking the Link Button, I am setting GridView Edit Index to 1 to make the row editable.
Now I want to save the updated row. On Click of another button on the Web Page, I want to save the updated changes and change the row edit mode to non-editable mode.
The best way to do this is using the specifics events, this way:
protected void gridview1_RowEditing(object sender, GridViewEditEventArgs e)
{
gridview1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void gridview1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gridview1.EditIndex = -1;
BindGrid();
}
To save use the event RowUpdating:
protected void gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gridview1.Rows[e.RowIndex];
int id = Convert.ToInt32(gridview1.DataKeys[e.RowIndex].Value);
string name = ((DropDownList)(row.Cells[2].Controls[1])).SelectedValue;
//call save method of your business layer
gridview1.EditIndex = -1;
BindGrid();
}
Remember to declare the event in the gridview markup.
I am using Entity Framework with ASPX webform. In my GridView(GV) I make all my columns with ItemTemplates and EditTemplates. When in edit mode I can select a new value, but it does not update the record. In the GV I have a DropDownList which is set to a EntityDataSource that matches it's related table for that field. What steps do I need, what events do I need to handle? I have tried the RowEditing and RowUpdating events, but have no useful code thus far. If you want me to show you some bad code - just ask and I will be more than happy too. I just need some guidance on wiring this up.
Assuming I have an ADO.NET entity data model called - customerEntities which has one table Customers with 3 columns:
CustomerId
Name
Surname
ASPX:
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateEditButton="true"
AutoGenerateColumns="false" onrowcancelingedit="gvCustomers_RowCancelingEdit"
onrowediting="gvCustomers_RowEditing" onrowupdating="gvCustomers_RowUpdating" DataKeyNames="CustomerId">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Bind("CustomerId") %>' />
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>' />
<asp:Label ID="lblSurname" runat="server" Text='<%# Bind("Surname") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' />
<asp:TextBox ID="txtSurname" runat="server" Text='<%# Bind("Surname") %>' />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindCustomers();
}
private void BindCustomers()
{
customerEntities entityModel = new customerEntities();
gvCustomers.DataSource = entityModel.Customers;
gvCustomers.DataBind();
}
protected void gvCustomers_RowEditing(object sender, GridViewEditEventArgs e)
{
gvCustomers.EditIndex = e.NewEditIndex;
BindCustomers();
}
protected void gvCustomers_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvCustomers.EditIndex = -1;
BindCustomers();
}
protected void gvCustomers_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int customerId = (int)gvCustomers.DataKeys[e.RowIndex].Value;
TextBox txtName = (TextBox)gvCustomers.Rows[e.RowIndex].FindControl("txtName");
TextBox txtSurname = (TextBox)gvCustomers.Rows[e.RowIndex].FindControl("txtSurname");
customerEntities entityModel = new customerEntities();
Customer customer = entityModel.Customers.Where(c => c.CustomerId == customerId).First();
customer.Name = txtName.Text;
customer.Surname = txtSurname.Text;
entityModel.SaveChanges();
gvCustomers.EditIndex = -1;
BindCustomers();
}
I have GridView with Template Column.Inside the template column i have asp:hiddenfield. I am binding the value using Eval() method.When i am trying to access the value of hiddenfi not accesible while visibility false
ASPX
<asp:TemplateField HeaderText="Select" Visible="false">
<ItemTemplate>
<asp:HiddenField ID="hdnMasterId" runat="server"
Value='<%# DataBinder.Eval(Container.DataItem, "Master_Id") %>' />
<asp:CheckBox ID="chkDelete" runat="server" />
</ItemTemplate>
<ItemStyle Width="4%" HorizontalAlign="Center"></ItemStyle>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
CODE BEHIND
protected void gdvList_RowCommand(object sender, GridViewCommandEventArgs e)
{
int intIndex = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gdvList.Rows[intIndex];
HiddenField hdn = (HiddenField)row.FindControl("hdnMasterId");
}
If you set visibility="false" on a column it won't generate any html, thus wont have the hidden control. You need to put the hiddenfield elsewhere or hide the column with css/style instead.
You could try as:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string strValue = ((HiddenField)GridView1.SelectedRow.Cells[cellindex].FindControl("HiddenFieldID")).Value;
}
How do I use a GridView TemplateField containing a LinkButton that is to display the modal on click? I have rows of data that I want to update the details of when clicking the 'Edit' LinkButton in that row. There is a bunch of data I need to load via codebehind just before the Modal displays.
I was trying the following, but I can't do Modal1.Show() in the event handler because it's in a TemplateField:
<ItemTemplate>
<asp:Button runat="server" ID="HiddenForModal" style="display: none" />
<ajaxToolkit:ModalPopupExtender ID="Modal1" runat="server" TargetControlID="HiddenForModal" PopupControlID="pnlModal" />
<asp:LinkButton ID="btnEdit" runat="server" Text="Edit" onclick="btnEdit_Click" />
<asp:LinkButton ID="btnDelete" runat="server" Text="Delete"></asp:LinkButton>
</ItemTemplate>
Thanks,
Mark
The key is knowing which row in the GridView was the LinkButton that was clicked. You can do this several ways but the way I implemented it is to capture it in the RowCommand event. Then you can access the ModalPopupExtender in the clicked row via FindControl(..).
Page:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" style="Display:none;" Text="Button" />
<cc1:ModalPopupExtender ID="ModalPopupExtender1" PopupControlID="Popup1" TargetControlID="Button1" BackgroundCssClass="modalBackground" runat="server" />
<asp:LinkButton ID="LinkButton1" CommandName="Popup" runat="server">Popup</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Codebehind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
LinkButton1.CommandArgument = e.Row.RowIndex.ToString();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Popup" && e.CommandArgument != null)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
ModalPopupExtender modalPopupExtender1 = (ModalPopupExtender)GridView1.Rows[rowIndex].FindControl("ModalPopupExtender1");
modalPopupExtender1.Show();
//Perform any specific processing.
Label1.Text = string.Format("Row # {0}", rowIndex);
}
}
Additionally, because you are opening the modal on a postback anyways you don't actually need the ModalPopupExtender (or the hidden button) in the ItemTemplate. You can move that out and put it on the page (by your popup div) and can simply call the Show() method.
Page:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandName="Popup" runat="server">Popup</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Codebehind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Popup" && e.CommandArgument != null)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
ModalPopupExtender1.Show();
//Perform any specific processing
Label1.Text = string.Format("<Br>Row # {0}", rowIndex);
}
}
Thanks, good luck!
I have a Gridview
<asp:GridView ID="GridView1" runat="server" Width="400px" AutoGenerateColumns="false"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="lblStudentName" runat="server" Text='<%# Eval("StudentName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblResidentialAddress" runat="server" Text='<%# Eval("ResidentialAddress") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and i get the value binded to the gridview
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt = objdb.GetData("Getsamples", new object[] { });
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
{
Response.Write(GridView1.SelectedRow.Cells[0].Text);
// string selectedText = ((Label)GridView1.SelectedRow.FindControl("lblStudentName")).Text;
// Response.Write(selectedText);
}
i cannot able to retrive the row where the checkbox is checked...
How to select particular row in a gridview, and based upon the selection i need to take out the 'Name' and pass this as a parameter to get ,another gridview related to the row which i selected.???
any help...
Try using another event - OnSelectedIndexChanging (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindexchanging.aspx)
It has GridViewSelectEventArgs (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewselecteventargs_members.aspx) passed to the event handler which has NewSelectedIndex property.
Your event handler will look like:
void GridView1_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
}