I am trying to develop an asp c# gridview that has select so that I can display additional details of the record. I also need a check box to allow the user the check the row for further processing. I can achieve each separately but not together. Is it even possible?
Of course it is possible.
For the column of check boxes, use TemplateField column, https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield%28v=vs.110%29.aspx
In data grid, aspx:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBoxProcess" runat="server" />
</ItemTemplate>
</asp:TemplateField>
In code behind:
protected void ButtonProcess_Click(object sender, EventArgs e)
{
foreach (GridViewRow item in GridView1.Rows)
{
CheckBox chk = (CheckBox)item.FindControl("CheckBoxProcess");
if (chk != null)
{
if (chk.Checked)
{
// This record should be processed
}
}
}
}
The GridView has build in row select functionality, you can enable it by setting AutoGenerateSelectButton property to true:
<asp:GridView ... AutoGenerateSelectButton="true" />
However, it is more user friendly to select the row when user clicks on it (rather than clicking on the link).
To do this, you need to attach a bit of java-script to the row:
void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Select$" + e.Row.RowIndex.ToString())
}
Here is also, much better explained solution for row click select:
https://stackoverflow.com/a/6250846/461810
You can use template fields and just add the controls into them.
<Columns>
<asp:TemplateField>
<ItemTemplate>
<select />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<checkbox />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="SelectChk" OnCheckedChanged="chk_Click1" runat="server" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
And in the CS code:
protected void chk_Click1(object sender, EventArgs e)
{
CheckBox MChk = sender as CheckBox;
GridViewRow MyGridR = MChk.NamingContainer as GridViewRow;
GridView1.SelectRow(MyGridR.RowIndex);
}
Related
is it possible to get CommandArgument from GridViewRow?
in the for-each loop in button click event I need CommandArgument
example code:
foreach (GridViewRow row in MyGridView.Rows)
{
...
string commandArgument = ?;
...
}
According to your comments I understood your mission . You can solve this
by creating Hidden Field for getting Id of table and you can access this id when the checkbox is checked. Then do update on the basis of id. Here I am updating
name column of grid view.
Sample:
WebForm1.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="lblUpdate" runat="server" Text= '<%#Eval("name") %>'></asp:Label>
<asp:CheckBox ID="chkbox" runat="server" />
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
WebForm1.aspx.cs
protected void btnSave_Click(object sender, EventArgs e) {
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType==DataControlRowType.DataRow)
{
CheckBox chkbox = (CheckBox)row.Cells[0].FindControl("chkbox");
HiddenField hdID = (HiddenField)row.Cells[0].FindControl("id");
Label lbl = (Label)row.Cells[0].FindControl("lblUpdate");
if (chkbox!=null)
{
if(chkbox.Checked)
{
string Id = hdID.Value;
//now update name... here by the help of this RowId===> Id
}
}
}
}
Note here Cells[0] means first TemplateField data. And I have used this because I have placed all name field and checkbox field in first templatefield.
I think this will help you. :)
I need control id from grid view to use Trigger.
My code is here :
<asp:GridView ID="gvDetails" CssClass="table table-striped table-bordered datatables dataTable" DataKeyNames="folder_path" CellPadding="5" runat="server" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="attachment_name" HeaderText="Attachment" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text="Download" runat="server" OnClick="DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-CssClass="hideGridColumn" ItemStyle-CssClass="hideGridColumn">
<ItemTemplate>
<asp:HiddenField ID="hdnAttach_Id" Value='<%#(Eval("attachment_id").ToString())%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#2FBDF1" Font-Bold="true" />
</asp:GridView>
I need code like...
<Triggers>
<asp:PostBackTrigger ControlID="lnkDownload" />
</Triggers>
how to get "lnkDownload" id from gridview?
Exception :
You need to register each and every LinkButton as an PostBackTrigger. After each row is bound in your GridView, you'll need to search for the LinkButton and register it through code as follows:
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton lb = e.Row.FindControl("lnkDownload") as LinkButton;
ScriptManager.GetCurrent(this).RegisterPostBackControl(lb);
}
And need to call this on RowDataBoundevent.
You can access the controls from the grid view using the findcontrol method
foreach(GridViewRow row in gvDetails.Rows)
{
if(row.RowType == DataControlRowType.DataRow)
{
LinkButton linkButton = (LinkButton )row.FindControl("lnkDownload");
//Your other code
}
}
I've recently encountered this problem as well.
You can find a control by doing e.row.findcontrol("NameOfControl").
Since I don't fully know what you intend to do, you can retrieve the ClientID by finding the control. Then you can specify the ID of the button by saying Button.ClientID. This will display the ContentHolder1_gridview_0_button_0.
To add attributes, you can do button.Attributes.Add("attribute", "#" + button.ClientID);
The following code is something I've fixed to have the following attribute added to the button. This is so that I can click a button and copy the textbox.
Example:
protected void gvListInventoryPassword_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HtmlButton buttonPass= (HtmlButton)e.Row.FindControl("buttonPass");
TextBox txtBox= (TextBox)e.Row.FindControl("txtBox");
buttonPass.Attributes.Add("data-clipboard-target", "#" + txtBox.ClientID);
}
}//End of gvListInventoryPassword_RowDataBound function
I have a GridView which show data retrived from database. I've made TemplateField (CheckBox) to GridView with this code:
<asp:GridView ID="dbRecordsContent" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="dbRecordsContent_SelectedIndexChanged">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="myCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:BoundField DataField="url" HeaderText="url" SortExpression="url" />
<asp:BoundField DataField="category" HeaderText="category" SortExpression="category" />
<asp:BoundField DataField="isChecked" HeaderText="isChecked" SortExpression="isChecked" />
</Columns>
</asp:GridView>
My grid view looks like this:
My question is: How do I know which checkbox ID is checked? For example:
I want to delete 2nd row when I press "Delete" button. Of course I will check second Checkbox, but how do I know which record to delete? How to reference second checkbox in a code?
In your delete button's click event handler you need to loop through all the rows in the grid and if a check box is checked, then you need to perform your delete logic, like this:
protected void DeleteButton_Click(object sender, EventArgs e)
{
foreach(GridViewRow row in dbRecordsContent.Rows)
{
// Only look for check boxes in data rows, ignoring header
// and footer rows
if (row.RowType == DataControlRowType.DataRow)
{
if (((CheckBox)row.FindControl("myCheckBox")).Checked)
{
// Do delete logic here
}
}
}
}
UPDATE:
To get the row number use the GridViewRow.RowIndex property, like this:
int rowNumber = row.RowIndex;
Read GridViewRow.RowIndex Property for more information.
try this,
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="myCheckBox" runat="server" AutoPostBack="true"
oncheckedchanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
code side,
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
int index = row.RowIndex;
CheckBox cb1 = (CheckBox)Gridview.Rows[index].FindControl("myCheckBox");
string checkboxstatus;
if (cb1.Checked)
{
//write your code
}
else
{
//write your code
}
}
I have a gridview in my ASPX page. This GridView is inside a UpdatePanel which causes partial page postbacks.
I have a button as the last column for each row in the GridView. How do I open temp.aspx page with ID value passed to it onClick of each button. I have a ID column in the gridView too.
Please help
You can get the DataSource for each row OnRowDataBound, then find the control you want in that row and apply your OnClick JavaScript:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Example"
<ItemTemplate>
<asp:label ID="YourSpanToClick" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label yourSpanToClick= (Label)e.Row.FindControl("YourSpanToClick");
YourDataSrcRowObject rowObject= DataBinder.Eval(e.Row.DataItem) as YourDataSrcRowObject ;
string js = String.Format("window.open('{0}');", "temp.aspx?Id=" + rowObject.Id)
yourSpanToClick.Attributes.Add("onclick",js);
}
}
You can't open an new window from OnClick event. Try the OnClientClick inside the RowDataBound event of that gridview.
Private Sub DataGridView1_OnRowDataBound(Sender as Object, e as DataGridView.EventArgs)
Dim btn as Button=e.Row.FindControl("btnName")
btn.OnClientClick="javascript:window.open(""temp.aspx?id=22"")
End Sub
Something like this.
Hi I need help implementing a logic using Gridview control in c#.
I have a gridview and it has many rows. Each row has a Button to click for the user. On each button click i am updating the selected record in the database. Now once row is updated, I need to hide that button to prevent reaction just for that particular row.
1. If i use this
<asp:CommandField ShowEditButton="True" EditText="select" />
, I can't make this hide.
2. If I use this
<asp:TemplateField HeaderText="Your Action">
<ItemTemplate>
<asp:Button
ID="btnAccept"
runat="server"
Text="Accept"
OnClientClick="return confirm('Are you sure you want to Accept this offer?');"
onclick="btnAccept_Click" />
</ItemTemplate>
</asp:TemplateField>
, I can't get the selected row index.
I hope i have cleared what i wanna ask. Thanks in advance.
Use Button control's CommandArgument property specify the row user clicked :
<asp:TemplateField HeaderText="Your Action">
<ItemTemplate>
<asp:Button
ID="btnAccept"
runat="server"
Text="Accept"
OnClientClick="return confirm('Are you sure you want to Accept this offer?');"
CommandName="Accept"
CommandArgument='<%# Eval("RowId") %>'
onclick="btnAccept_Click" />
</ItemTemplate>
</asp:TemplateField>
At code behind :
void btnAccept_Click(Object sender, CommandEventArgs e)
{
if (e.CommandName == "Accept")
{
string rowId = e.CommandArgument;
}
}
Continuing on Canavars solution for 1):
void btnAccept_Click(Object sender, CommandEventArgs e)
{
if (e.CommandName == "Accept")
{
string rowId = e.CommandArgument;
((Button)sender).Visible = false;
}
}