I have a GridView that displays a list and the user can select a checkbox for each item.
So for example I check the second row. I can see in the database that the check value next to this description has updated to 1:
But when I go back into the GridView, all the checkboxes are blank again.
Code for GridView:
<asp:GridView style="width:75%"
ID="gvCVRTDetails"
ShowHeaderWhenEmpty="true"
CssClass="tblResults"
runat="server"
OnRowDataBound="gvCVRTDetails_RowDataBound"
DataKeyField="ID"
AutoGenerateColumns="false"
allowpaging="false"
AlternatingRowStyle-BackColor="#EEEEEE">
<HeaderStyle CssClass="tblResultsHeader" />
<Columns>
<asp:BoundField DataField="ChecklistID" HeaderText="ID" ></asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Checklist Items"></asp:BoundField>
<asp:TemplateField HeaderText ="Checked?" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkChecked" runat="server" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
protected void gvCVRTDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
lookupCVRT work = (lookupCVRT)e.Row.DataItem;
GridView gv = sender as GridView;
e.Row.Attributes.Add("ID", "gvCVRTDetails_" + work.ID);
e.Row.Cells[0].Attributes.Add("onclick", "event.stopPropagation();");
CheckBox chkChecked = e.Row.FindControl("chkChecked") as CheckBox;
if(work.Checked)
{
chkChecked.Checked = true;
}
}
}
I tried setting chkChecked.Checked = true; if there is a value for the Checked field in the database but that didn't work. How do I get the checkboxes to show as ticked if the value in the database is equal to 1?
Binding the Grid:
protected void gridviewParent_SelectedIndexChanged(object sender, EventArgs e)
{
List<lookupCVRT> workDetails = lookupCVRT.GetChecklistItemsByChecklistID(Company.Current.CompanyID, ParentID.ToString(), gvCVRT.SelectedDataKey.Value.ToString());
gvCVRTDetails.DataSource = workDetails;
gvCVRTDetails.DataBind();
FireJavascriptCallback("setArgAndPostBack ();");
}
You should bind the checkbox to the property that you read from db, something like:
<asp:TemplateField HeaderText ="Checked?" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkChecked" Checked='<%# Bind("YourCheckPropertyFromModel") %>'runat="server" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
Related
I have one checkbox with ID=checkbox1 in Asp.net gridview. I want to use checkbox1 in if else condition in backend C# code.My problem is I am not getting how to call checkbox1 as it is not showing in the backend.
Thanks in advance.
My code is
if (txtsender.Text != String.Empty && checkbox1.checked==true)
{
chklist();
}
One view to access your checkbox inside Gridview is to loop on grid view rows and find your checkbox. Code example given below to access gridview checkbox in button click event
Aspx code
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="ID" DataField="ID" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkBox" runat="server" AutoPostBack="true" Text="T1" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btn" runat="server" OnClick="btn_Click" Text="Submit" />
Aspx.cs Code
protected void btn_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gv1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox c = (CheckBox)row.FindControl("chkBox");
if (c.Checked)
{
}
}
}
}
I'm trying to populate multiple text boxes with data from a gridview when I click the link button (which is in fact the name of one of the fields in each row) but it isn't going through. I'm new to this - literally my first time. Any help would be most appreciated.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = GridView1.Rows[index];
AccountNumber.Text = selectedRow.Cells[1].Text;
Name.Text = selectedRow.Cells[1].Text;
Address1.Text = selectedRow.Cells[1].Text;
Address2.Text = selectedRow.Cells[2].Text;
Address3.Text = selectedRow.Cells[3].Text;
PhoneNumber.Text = selectedRow.Cells[4].Text;
FaxNumber.Text= selectedRow.Cells[5].Text;
CurrencyID.Text = selectedRow.Cells[6].Text;
}
}
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="Agent_Account_No"
DataSourceID="SqlDataSource1"
AlternatingRowStyle-BackColor ="Lavender"
HeaderStyle-BackColor="#9966FF"
AllowSorting="True" HeaderStyle-BorderColor="Black"
HorizontalAlign="Center"
RowStyle-BorderColor="Black"
EmptyDataText="There are no data records to display."
onrowcommand ="GridView1_RowCommand">
<AlternatingRowStyle BackColor="#CCFFCC" />
<Columns>
<asp:BoundField datafield="Agent_Account_No" HeaderText="Account No"
ItemStyle-HorizontalAlign="Center"
ItemStyle-VerticalAlign="Middle"
ItemStyle-Width="50"
SortExpression="Agent_Account_No"
ReadOnly="true">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"
Width="50px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Name" SortExpression="Agent_Name">
<ItemTemplate>
<asp:LinkButton ID="AgentName" runat="server"
Text='<%# Eval("Agent_Name") %>'
CommandName="Select"
CommandArgument='<%#Bind("Agent_Name") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
I got it to work this way - using help from this site:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
AccountNumber.Text = GridView1.DataKeys[row.RowIndex]["Agent_Account_No"].ToString();
......
}
I don't know if this declaration is right, but it works. However now that it works I see a problem that I didn't see before - see my profile and thanks a lot!
I would highly recommend using TemplateFields for all of your columns, like this:
Markup:
<Columns>
<asp:TemplateField HeaderText="Account No" SortExpression="Agent_Account_No">
<ItemTemplate>
<asp:Label ID="LabelAccountNumber" runat="server"
Text='<%# Eval("Agent_Account_No") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
...
<asp:TemplateField HeaderText="Name" SortExpression="Agent_Name">
<ItemTemplate>
<asp:LinkButton ID="AgentName" runat="server"
Text='<%# Eval("Agent_Name") %>'
CommandName="Select"
CommandArgument='<%#Bind("Agent_Name") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Now in the RowCommand method, you can use the FindControl() method of the grid view row to get to the text you are interested in, like this:
Code-behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = GridView1.Rows[index];
// Find the account number label to get the text value for the text box
Label theAccountNumberLabel = selectedRow.FindControl("LabelAccountNumber") as Label;
// Make sure we found the label before we try to use it
if(theAccountNumberLabel != null)
{
AccountNumber.Text = theAccountNumberLabel.Text;
}
// Follow the same pattern for the other labels to get other text values
}
}
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
}
}
In my GridView, I have the following columns:
<Columns>
<asp:BoundField DataField="report_type" HeaderText="Report Type"
SortExpression="report_type" />
<asp:BoundField DataField="progress" HeaderText="Progress"
SortExpression="progress" />
<asp:TemplateField HeaderText="..">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataValueField="progress">
<asp:ListItem Value="0">Incomplete</asp:ListItem>
<asp:ListItem Value="1">Complete</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
The progress column is just there for demo purposes which will eventually be removed. How do I get the value of the progress to select the correct itemlist in the dropdown?
So if the value of progress is 1, the dropdown should have Complete selected. If the value of the progress is 0, the dropdown should have Incomplete selected.
Add a OnRowDataBound attribute to the gridview in the .aspx page:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" OnRowDataBound="GridViewRowEventHandler">
Replace
<asp:BoundField DataField="Progress" HeaderText="Progress"
SortExpression="progress" />
with
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="progress_Flags" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Progress").ToString()%>'/>
</ItemTemplate>
</asp:TemplateField>
In the code behind:
protected void GridViewRowEventHandler(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label flag = (Label)e.Row.FindControl("progress_Flags");
DropDownList myDropDown = (DropDownList)e.Row.FindControl("DropDownList1");
if (flag.Text == "1")
{
myDropDown.SelectedValue = "1";
}
//add more conditions here..
}
}
In RowDataBound event you can use e.Row.FindControl
protected void GridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
DataRowView dr = row.DataItem as DataRowView;
// now find the control in the row by control ID
DropDownList myDropDown = row.FindControl("DropDownList1") as DropDownList;
}
I have this gridview:
<div class="content">
<asp:GridView ID="DocumentGrid" runat="server" AutoGenerateColumns="False" OnRowCommand="DocumentGrid_RowCommand" >
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" ItemStyle-Width="120px"/>
<asp:ButtonField HeaderText="Download Link" Text="Download"/>
</Columns>
</asp:GridView>
</div>
As you can see, DocumentGrid_RowCommand is called when the "Download" button is pressed, How can I find out what the values are of the row that was clicked?
If more than one button fields are there in GridView, set CommandName attribute. That way we can determine which button is pressed in RowCommand event. So always set commandName attribute.
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" ItemStyle-Width="120px"/>
<asp:ButtonField HeaderText="Download Link" Text="Download" CommandName="cmd"/>
</Columns>
In RowCommand event handler, GridViewCommandEventArgs.CommandArgument property returns index of row on which button is pressed.
protected void DocumentGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmd")
{
int index = int.Parse(e.CommandArgument.ToString());
GridViewRow row = DocumentGrid.Rows[index];
if (row.RowType == DataControlRowType.DataRow)
{
Response.Write(row.Cells[0].Text);
}
}
}
If you set the markup like this,
<Columns>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:Button ID="btnDownload" CommandName="Download" CommandArgument='<%# Container.DataItemIndex %>'
runat="server" Text="Download" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
on the code-behind you can check the CommandArgument like this:
if (e.CommandName == "Download")
{
int index = Convert.ToInt32(e.CommandArgument);
}