So, I am creating a basic CRUD system, where you can register clients, and I want to make every textbox a required field, but I don't know how to do that.
Can someone help me ?
My code :
<asp:BoundField DataField="cliente_id" HeaderText="Id do Cliente" ReadOnly="true" />
<asp:TemplateField HeaderText="Nome do Cliente">
<ItemTemplate>
<asp:Label ID="lblClienteNome" runat="server" Text='<%# Eval("cliente_nome")%>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtclienteNome" runat="server" Text='<%# Eval("cliente_nome")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtbnome" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "E-mail do Cliente">
<ItemTemplate>
<asp:Label ID="lblClienteEmail" runat="server" Text='<%# Eval("cliente_email")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtclienteEmail" runat="server" Text='<%# Eval("cliente_email")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtbemail" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Data de Nascimento">
<ItemTemplate>
<asp:Label ID="lbldataNascimento" runat="server" Text='<%# Eval("cliente_dataNascimento")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtdataNascimento" runat="server" Text='<%# Eval("cliente_dataNascimento")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtbdataNascimento" runat="server" />
<asp:LinkButton ID="btnAdd" CommandName="AddNew" runat="server" CssClass="btn btn-large btn-info pull-right"><i class="glyphicon glyphicon-plus"></i> Adicionar</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowDeleteButton="true" />
</Columns>
Simple way to do is using adding the following code under all the textbox
<asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="textboxid" errormessage="Please enter your Value!" />
Related
i want edit and delete button on the right side of the gridview not
at left side of grid. for user friendly manipulation of gridview.
how to show buttons at right side of my grid view?
<asp:GridView ID="GvAllTasks" runat="server" AutoGenerateColumns="false"
AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"
CssClass="table table-hover table-bordered"
onrowcancelingedit="GvAllTasks_RowCancelingEdit"
onrowdeleting="GvAllTasks_RowDeleting"
onrowediting="GvAllTasks_RowEditing" onrowupdating="GvAllTasks_RowUpdating" ShowHeaderWhenEmpty="True" EmptyDataText="No records Found">
you need to set AutoGenerateColumns property false and create the required columns for the grid using property within the html source of the grid in whatever order you want. The columns can be pre defined columns like CheckBoxField, ImageField etc. or templatecolumns. The following example shows creating a grid with edit and delete buttons on right for a table with name DEPT having three columns DEPTNO, DNAME and LOC.
<asp:GridView ID="GvDept" runat="server" AutoGenerateColumns="False"
ShowFooter="true"style="z-index: 1; left: 145px; top: 22px;position:
absolute; height: 191px; width: 756px;font-family: 'Arial Black';
font-size: large" EnableViewState="False">
<Columns>
<asp:TemplateField HeaderText="Deptno">
<ItemTemplate>
<asp:Label ID="LblDno" runat="server"
Text='<%# Eval("Deptno") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dept Name">
<ItemTemplate>
<asp:Label ID="LblDname" runat="server"
Text='<%# Eval("Dname") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:Label ID="LblLoc" runat="server"
Text='<%# Eval("Loc") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="LnkEdit" runat="server" Text="Edit"
CommandName="Edit" />
<asp:LinkButton ID="LnkDelete" runat="server"
Text="Delete" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
for adding edit and delete buttons left side of ur gridview add
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
in your columns after your template field
CssClass="table table-hover table-bordered"
onrowcancelingedit="GvAllTasks_RowCancelingEdit"
onrowdeleting="GvAllTasks_RowDeleting"
onrowediting="GvAllTasks_RowEditing" onrowupdating="GvAllTasks_RowUpdating" ShowHeaderWhenEmpty="True" EmptyDataText="No records Found">
<%-- AutoGenerateEditButton="true"--%>
<%-- AutoGenerateDeleteButton="true" --%>
<Columns>
<asp:TemplateField HeaderText="Task Id">
<ItemTemplate>
<asp:HiddenField ID="HiddTaskid" runat="server" Value='<%# Eval("task_Id") %>' />
<%#Eval("task_Id")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Name">
<ItemTemplate>
<%#Eval("task_Name")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assigned To">
<ItemTemplate>
<%#Eval("task_Employee_Name")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Name">
<ItemTemplate>
<%#Eval("task_Project_Title")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Status">
<ItemTemplate>
<%#Eval("task_Status")%>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lbl_Status" runat="server" Text='<%#"Status : " + Eval("task_Status") %>'></asp:Label>
<asp:DropDownList ID="Gv_DdlStatus" runat="server">
<asp:ListItem Text="-Change Status-" Value="0"></asp:ListItem>
<asp:ListItem Text="Reviewed" Value="1"></asp:ListItem>
<asp:ListItem Text="InProgress" Value="1"></asp:ListItem>
<asp:ListItem Text="Completed" Value="2"></asp:ListItem>
<asp:ListItem Text="Stopped" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:HiddenField ID="hiddstatusId" runat="server" Value='<%# Eval("task_Status") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Start dt">
<ItemTemplate>
<%#Eval("task_StartDate")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Completed dt">
<ItemTemplate>
<%#Eval("TaskCompletedDt")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTaskCompletedDt" runat="server" Text='<%# Eval("TaskCompletedDt") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assigned By">
<ItemTemplate>
<%#Eval("AssignedBy")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments">
<ItemTemplate>
<%#Eval("comment")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtComments" runat="server" Text='<%# Eval("comment") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
error- Object reference not set to an instance of an object.
The error is pointing towards RadioButtonList which is present inside Footer Row of GridView. Object reference not set to an instance of an object.
Description: An unhanded exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 69:
Line 70: //for RadioButtonLiist Control
Line 71: gender = ((RadioButtonList) (this.GridView1.FooterRow.FindControl("RadioButtonList2"))).SelectedValue;
Line 72:
Line 73: //for DropDownList
Code-
string name, city, gender;
//id = int.Parse(((TextBox)this.GridView1.FooterRow.FindControl("TextBox1"))).Text);
name = ((TextBox)(this.GridView1.FooterRow.FindControl("TextBox5"))).Text;
//for RadioButtonLiist Control
gender = ((RadioButtonList)(this.GridView1.FooterRow.FindControl("RadioButtonList2"))).SelectedValue;
city = ((TextBox)(this.GridView1.FooterRow.FindControl("TextBox4"))).Text;
cmd = new SqlCommand("insert into emp values('" + name + "','" + gender + "','" + city + "')", cn);
cmd.ExecuteNonQuery();
disp();
Code Behind-
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" ShowFooter="True"
onrowcancelingedit="GridView1_RowCancelingEdit">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField HeaderText="Id">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# bind("name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="gender">
<EditItemTemplate>
<br />
<asp:DropDownList ID="DropDownList1" SelectedValue='<%# bind("gender") %>' runat="server">
<asp:ListItem>male</asp:ListItem>
<asp:ListItem>female</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:RadioButtonList ID="RadiaButtonList2" runat="server" Height="18px"
Width="84px">
<asp:ListItem>male</asp:ListItem>
<asp:ListItem>female</asp:ListItem>
</asp:RadioButtonList>
</FooterTemplate>
<ItemTemplate>
<br />
<asp:Label ID="Label4" runat="server" Text='<%# bind("gender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="city">
<EditItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem>lucknow</asp:ListItem>
<asp:ListItem>kanpur</asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# bind("city") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Add new item">
<FooterTemplate>
<asp:LinkButton ID="lbInsert" runat="server" onclick="LinkButton1_Click">Insert</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is because in the following code, you have mis-spelled RadioButtonList2 :-
<asp:RadioButtonList ID="RadiaButtonList2" runat="server" Height="18px"
Width="84px">
<asp:ListItem>male</asp:ListItem>
<asp:ListItem>female</asp:ListItem>
</asp:RadioButtonList>
Thus, it is unable to find that control and you are trying to invoke a property on a Null reference!
I have a gridView that I wish to add a textbox to. I want the textbox to be located in the footer, but I have no idea how to do that.
This is my gridview:
<asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false"
AllowSorting="True"
ShowFooter="True"
ShowHeaderWhenEmpty="true"
OnRowDataBound="gvTest_RowDataBound"
Width="550px">
<EmptyDataTemplate>
No data.
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="Name" headerText="Name"/>
<asp:TemplateField>
<HeaderTemplate>
Actions
</HeaderTemplate>
<ItemTemplate>
<asp:ImageButton ID="imbView" runat="server" ToolTip="View details" ImageUrl="~/css/images/Search.png" Width="16" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I've seen people talking about adding something like this:
<FooterTemplate>
<asp:TextBox ID="tbName" runat="server"/>
</FooterTemplate>
But I dont know where or how to add it. If I just put it in between the <asp:TemplateField>tags it messes up (probably because my boundfields).
Additional information:
I bind the gridview using DataTable. The real table has more columns, but this will suffice for an example.
Switch from:
<asp:TemplateField>
<HeaderTemplate>
Actions
</HeaderTemplate>
<ItemTemplate>
<asp:ImageButton ID="imbView" runat="server" ToolTip="View details" ImageUrl="~/css/images/Search.png" Width="16" />
</ItemTemplate>
</asp:TemplateField>
to:
<asp:TemplateField>
<HeaderTemplate>
Actions
</HeaderTemplate>
<ItemTemplate>
<asp:ImageButton ID="imbView" runat="server" ToolTip="View details" ImageUrl="~/css/images/Search.png" Width="16" />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbName" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
It's easy if you understand, that you can devide your TemplateField into three sections:
Header
Item(or main content)
Footer
Greetz
You will need to put it in the template field.. like this..
<asp:TemplateField HeaderText="BankName" SortExpression="BankName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("BankName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("BankName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbAddBankName" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
this is an example from my working code.
Dropdownlist list is not working in my following grid.I want to change label text when drop downlist changed dropdownlisth the label text is not working
<asp:TemplateField HeaderText="sup_rec_id">
<ItemTemplate>
<asp:Label runat="server" ID="lblFirstLastName" Text='<%# Eval("sup_rec_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:Label runat="server" ID="quantity" Text='<%# Eval("quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Code">
<ItemTemplate>
<asp:Label runat="server" ID="code" Text='<%# Eval("code") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="true" EnableViewState="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
<asp:ListItem>D</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
filename.Aspx.cs
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = "Test text";
}
Guide me what i do?
I have a Master Page which contain a Script Manager.In a child page i have a scrollable gridview with checkbox and some textboxes
<div id="grdScr" style ="width:130%; overflow:auto" align="center" >
<asp:GridView ID="dgvBill" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="4" ForeColor="Black" GridLines="Horizontal" PageSize="100"
Width="150%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="True"
oncheckedchanged="chkSelect_CheckedChanged" />
<asp:Label ID="lblVendCd" runat="server" Text='<%# Eval("vend_cd") %>'
Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bill date">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("bill_dt") %>'></asp:Label>
<asp:Label ID="lbltrid" runat="server" Text='<%# Eval("tr_id") %>'
Visible="False"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bill no">
<ItemTemplate>
<asp:Label ID="lblBillNO" runat="server" Text='<%# Eval("bill_no") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vendor">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("vend_nm") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Supplier">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("supplier") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Driver">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("driver") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vechicle Type">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("comm_nm") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vehicle No">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("veh_no") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Payable Amount">
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Eval("payable") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cash">
<ItemTemplate>
<asp:TextBox ID="txtCashGrid" runat="server" AutoPostBack="True"
Enabled="False" ontextchanged="txtCashGrid_TextChanged"
style="text-align: right" Width="75px" onkeypress="return isNumberKey(event)">0.00</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cheque">
<ItemTemplate>
<asp:TextBox ID="txtChequeGrid" runat="server" AutoPostBack="True"
Enabled="False" ontextchanged="txtChequeGrid_TextChanged"
style="text-align: right" Width="75px" onkeypress="return isNumberKey(event)">0.00</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bank">
<ItemTemplate>
<asp:TextBox ID="txtBankNameGrid" runat="server" Width="174px"
AutoPostBack="True" ontextchanged="txtBankNameGrid_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cheque No">
<ItemTemplate>
<asp:TextBox ID="txtChqNoGrid" runat="server" Width="75px" AutoPostBack="True"
ontextchanged="txtChqNoGrid_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cheque Date">
<ItemTemplate>
<asp:TextBox ID="txtChDateGrid" runat="server" AutoPostBack="True"
EnableTheming="True" Width="97px"
ontextchanged="txtChDateGrid_TextChanged"></asp:TextBox>
<cc1:TextBoxWatermarkExtender ID="txtChDateGrid_TextBoxWatermarkExtender"
runat="server" Enabled="True" TargetControlID="txtChDateGrid"
WatermarkText="dd-MMM-yyyy">
</cc1:TextBoxWatermarkExtender>
<cc1:CalendarExtender ID="txtChDateGrid_CalendarExtender" runat="server"
Enabled="True" Format="dd-MMM-yyyy" TargetControlID="txtChDateGrid">
</cc1:CalendarExtender>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White"
HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#CCCCCC" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
</asp:GridView>
</div>
My gridview is inside an update panel. The textboxes are doing some mathematical calculations in side the gridview in their TextChange Event and autopostback is true for them.
Whenever I am pressing tab for moving the cursor to the next textbox the page gets postbacked and the scroll position is not maintained. How will I solve this?