I have the following markup in an ASP.NET page:
<asp:GridView ID="gv" runat="server" DataSourceID="ods" OnRowDataBound="gv_RowDataBound">
<columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cb" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="tbx" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
How can I validate the TextBox only if the CheckBox is checked?
I searched for similar situations, but couldn't find anything.
Thanks.
U can use RowCommand under which u can get the control status using int index = AuthorsGridView.EditIndex;
GridViewRow row = AuthorsGridView.Rows[index]; and then validate the text box
refer this http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.aspx
Related
How to set the CheckBox value, which is located inside a Gridview ?
<asp:GridView ID="gviewPermission" runat="server"
onrowdatabound="gviewPermission_RowDataBound"
onrowupdated="gviewPermission_RowUpdated"
onrowupdating="gviewPermission_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Allow" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="Check_Allow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Deny" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="Check_Deny" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The check box value has to set based upon some condition....
In the gviewPermission_RowDataBound function do:
if(e.Row.RowType == DataControlRowType.DataRow)
((CheckBox)e.Row.FindControl("Check_Allow")).Checked = SomeCondition;
Or if the condition is coming directly from the datasource you can do:
<ItemTemplate>
<asp:CheckBox ID="Check_Allow" runat="server"
Checked='<%# Eval("ConditionFromDs") %>' />
</ItemTemplate>
If the value of column is boolean then. Try the below code
<ItemTemplate>
<center>
<asp:CheckBox ID="chkSelect" Checked='<%#Convert.ToBoolean(Eval("isChecked"))%>' runat="server"></asp:CheckBox></center>
</ItemTemplate>
Where "isChecked" is the column name.
the CheckBox control has an attribute called Checked that works similarly to the html counterpart attribute. So set this attribute either in the aspx markup:
<asp:CheckBox ID="Check_Allow" runat="server" Checked='<%= someCondition == true %>' />
or in your code-behind.
<ItemTemplate>
<asp:CheckBox runat="server" checked='<%# bool.Parse(Eval("check").ToString()) %>' ID="chkselet" />
</ItemTemplate>
check value must be true or false
I have a datagrid with a set of columns showing data from a database. I create the datatable and add it to the datagrid and then bind the source. this works great and now I would like to add a column to the front of the grid that has checkbox in it.
Do I add the checkbox when I am adding the new row to the datatable that is shown in the datagrid or after I databind the datatable to the datagrid?
Using: VB.Net, Visual Studio 2012
you can add checkbox using template field
Set AutoGenerateColumns attribute to false.
Add Column tag to asp:DataGrid tag.
Now add itemtemplate inside columns
<asp:DataGrid ID="DefaultGrid" Runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="chkAll" type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:DataGrid>
and if you want to attach it to datatable column then u have to add like this
<asp:DataGrid ID="DefaultGrid" Runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkStatus_OnChackedChanged" Checked='<%# Convert.ToBoolean(Eval("Approved")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:DataGrid>
Today i was come to an issue when i was deleting a record on basis of id from grid view and i used OnRowCommand event for this.
here is my gridview code :
<asp:GridView ID="gridShow" runat="server" AutoGenerateColumns="False" PageSize="5"
AllowPaging="true" ShowHeader="false" OnRowCommand="s_index" OnRowDeleting="gridShow_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtID" runat="server" Text='<%#Eval("ID") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%">
<ItemTemplate>
<asp:TextBox ID="txtDescription" runat="server" Text='<%#Eval("RollNumber") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtFname" runat="server" Text='<%#Eval("FirstName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%">
<ItemTemplate>
<asp:TextBox ID="txtLname" runat="server" Text='<%#Eval("LastName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%">
<ItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" Text='<%#Eval("Email") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"></asp:Label>
<asp:HiddenField ID="hdnStatus" runat="server" Value='<%#Eval("UserName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb" runat="server" Text="Delete" CommandName="delete" CommandArgument='<%#Eval("ID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is my C# code :
protected void s_index(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
oSRegisterBLL.BLLdelete(Convert.ToInt32(e.CommandArgument));
gview();
}
}
protected void gridShow_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
I successfully did this task by adding OnRowDeleting event on my grid view and definition of that event on my page behind but when i removed this first time i have come to known and issue " ASP.datashow_aspx' does not contain a definition for 'gridShow_RowDeleting' and no extension method 'gridShow_RowDeleting' accepting a first argument of type 'ASP.datashow_aspx' could be found (are you missing a using directive or an assembly reference?) "
I am confused with that why to add OnRowDeleting event on grid view with onrowcommand event ?
why i am confused because if i did not do any work with this event then why to use this event?
is there any way to work with only onrowcommand event ? or adding onrowdeleting event is essential for deletion of records from gridview?
I want to clear my mind for this ?
Your aspx markup of the GridView has declared the event handler here:
OnRowDeleting="gridShow_RowDeleting"
So when you try to remove it from the coedebehind you'll get that exception. So simply remove the event-handler and you can remove it from the codebehind.
Edit
Having a Delete-button, or even a regular button in a GridView with a CommandName of delete(which is the case here), will automatically try to fire OnRowDeleting.
So you have to add the event handler even if you don't use it or you have to rename the CommandName to e.g. "DeleteUser", otherwise you get exceptions like "The GridView 'gridShow' fired event RowDeleting which wasn't handled".
I have a GridView that is made up of two database fields populated via a stored procedure and then three fields for user input (two checkbox controls and one textbox) when I click on the save button I can get the information from the three controls but nothing from the two that were populated via the database. How can I get the first two fields?
<asp:GridView ID="gvA1" runat="server" AutoGenerateColumns="false" DataKeyNames="CodeNo" AutoGenerateSelectButton="false" EnablePersistedSelection="True" Visible="false">
<Columns>
<asp:TemplateField Visible="false" >
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "CodeNo")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Wrap="true" ItemStyle-Width="400px" HeaderText="Violation">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "CodeViolationCited") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="A1Accordion_cbPool" runat="server" Text="Pool:" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="A1Accordion_cbSpa" runat="server" Text="Spa:" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Additional Comments" Visible="true">
<ItemTemplate>
<asp:TextBox ID="A1Accordion_tb" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
protected void SaveAndCollapseA1(object sender, EventArgs e)
{
//good stuff
CheckBox myCheckBox_1 = gvA1.Rows[0].FindControl("A1Accordion_cbPool") as CheckBox;
CheckBox myCheckBox_2 = gvA1.Rows[0].FindControl("A1Accordion_cbSpa") as CheckBox;
TextBox myTextBox = gvA1.Rows[0].FindControl("A1Accordion_tb") as TextBox;
//not so good stuff
String myString1 = gvA1.Rows[0].Cells[0].ToString();
String myString2 = gvA1.Rows[0].Cells[1].ToString();
}
I figured it out but I haven't been hear long enough to post the solution via the links but if you change the columns as so:
<asp:label ID="lblA1CodeNo" runat="server" text='<%# Eval("CodeNo") %>'></asp:label>
then the values are available...
Try:
<%# Bind("CodeViolationCited") %>
Instead of
<%#DataBinder.Eval(Container.DataItem, "CodeViolationCited") %>
Eval is one way, bind is two-way.
See Microsoft's documentation for data-binding expressions
Lets say I have a drop down list and a grid view on a page like this
<asp:GridView ID="gvCategories" runat="server" >
<Columns>
<asp:HyperLinkField DataTextField="CategoryName" DataNavigateUrlFields="CategoryID" DataNavigateUrlFormatString="~/Learning.aspx?categoryID={0" />
</Columns>
</asp:GridView>
I'd like to add this to the URL:
&view=<%=SelectedDropdownlistvalue%>
How can I do this?
I would use TemplateField as:
<asp:GridView ID="gvCategories" runat="server" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("CategoryName", "~/Learning.aspx?categoryID={0}")+" &view=" + DropDownList1.SelectedValue %>'
Text="Goto Page"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Remember the DropDownList.SelectedValue is obtained at postBack, so the link is constructed at that time. To change the link dynamically when the value of the dropdownlist changes set the AutoPostBack="true" of the dropdownlist.