How to assign RequiredFiled Validator for DropDownList in Asp.Net
Ans:
<form id="form1" runat="server">
<div>
Select Gender :
<asp:DropDownList runat="server" ID="ddlList">
<asp:ListItem Text="Select" Value="0"></asp:ListItem>
<asp:ListItem Text="Male" Value="1"></asp:ListItem>
<asp:ListItem Text="Female" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ID="rfv" ControlToValidate="ddlList" ForeColor="Red" InitialValue="0" ErrorMessage="Please Select Gender"></asp:RequiredFieldValidator><br />
<asp:Button runat="server" ID="btnsubmit" Text="Submit" />
</div>
</form>
Output:How to add items to DropDownList through .Cs file
Ans: In .aspx file
<form id="form1" runat="server">
<div>
Select Gender :
<asp:DropDownList runat="server" ID="DropDownList1">
</asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" ControlToValidate="DropDownList1" ForeColor="Red" InitialValue="0" ErrorMessage="Please Select Gender"></asp:RequiredFieldValidator><br />
<asp:Button runat="server" ID="Button1" Text="Submit" />
</div>
</form>
In .Cs file protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListItem item1 = new ListItem("Select", "0");
ListItem item2 = new ListItem("Male", "1");
ListItem item3 = new ListItem("Female", "2");
DropDownList1.Items.Insert(0, item1);
DropDownList1.Items.Insert(1, item2);
DropDownList1.Items.Insert(2, item3);
}
}
Output:
How to Get DataKeyNames from GridView using RowUpdating Event and Button Click Event of Gridview
Ans:
In RowUpdating Event of GridView
protected void Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Suppose if DataKeyName is type Integer
Int32 DataKeyInt = Convert.ToInt32(Gridview1.DataKeys[e.RowIndex].Value.ToString());
//Suppose if DataKeyName is type String
String DataKeyString = Gridview1.DataKeys[e.RowIndex].Value.ToString();
}
In Button Click Event in GridView
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
//Suppose if DataKeyName is type Integer
Int32 DataKeyInt = Convert.ToInt32(Gridview1.DataKeys[row.RowIndex].Value.ToString());
//Suppose if DataKeyName is type String
String DataKeyString = Gridview1.DataKeys[row.RowIndex].Value.ToString();
}
How to find Control in GridView Using RowUpdating Event and RowDataBound Event
Ans:
In RowUpdating Event
protected void Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Suppose if we want to find TextBox Control from GridView
TextBox txt = (TextBox)Gridview1.Rows[e.RowIndex].FindControl("txtboxID");
string textboxvalue = txt.Text;
//Suppose if we want to find Label Control from GridView
Label lbl = (Label)Gridview1.Rows[e.RowIndex].FindControl("lblID");
string lblvalue = lbl.Text;
}
In RowDataBound Event
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Suppose if we want to find TextBox Control from GridView
TextBox txt = Gridview1.FindControl("txtID") as TextBox;
string textboxvalue = txt.Text;
//Suppose if we want to find Label Control from GridView
Label lbl = Gridview1.FindControl("lblID") as Label;
string lblvalue = lbl.Text;
}
No comments:
Post a Comment