I have a repeater control which has a DropDownlist and a TextBox.
On selectedIndexChanged event of the dropdown, I need to enable or disable the textbox based on the dropdown selected value.
I am able to trigger the SelectedIndexChanged event but not able to find the TextBox control inside this event.
Below is my SelectedIndexChanged event handler in codebehind.
protected void ddl_userType_SelectedIndex(object sender, EventArgs e)
{
var ddl = (DropDownList)sender;
}
Could someone please help me achieve what I need.
Hope about something like this...
protected void ddl_userType_SelectedIndex(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
RepeaterItem item = ddl.Parent as RepeaterItem;
Textbox tb = item.FindControl("MyTextBox") as Textbox;
}
Related
I'm little bit confusion, how to get value of TextBox of GridView in RowEditing and I've textchange event and that textbox value need to update via gridview to database. but before updating we need to calculate that value.
In RowUpdating we get value normally but in function calculationA() i'm not getting value of textbox. and need to calculate that value and show edited value in same textbox also.
public void calculationA()
{
TextBox txt_BCICU = (TextBox)grdlist.FindControl("txt_BCICU");
TextBox txt_BCSupDlx = (TextBox)grdlist.FindControl("txt_BCSupDlx");
txt_TotalChargeA.Text = (Convert.ToDecimal(txt_BCSupDlx.Text.Trim()) + Convert.ToDecimal(txt_BCICU.Text.Trim())).ToString();
protected void txt_BCICU_TextChanged(object sender, EventArgs e)
{
calculationA();
}
protected void grdlist_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txt_BCICU = (TextBox)grdlist.Rows[e.RowIndex].FindControl("txt_BCICU");
TextBox txt_BCSupDlx = (TextBox)grdlist.Rows[e.RowIndex].FindControl("txt_BCSupDlx");
}
APPROACH 1
You don't need TextChanged event to get value of textbox in gridview.
You can get the textbox value in RowUpdating event as in code below.
Also, remove the calculationA method and instead use the last line of code I have given in RowUpdating event.
protected void grdlist_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string textBox1Text = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_BCICU")).Text;
string textBox2Text = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_BCSupDlx")).Text;
//remove the calculationA function and just use the code below in
//RowUpdating event
txt_TotalChargeA.Text = (Convert.ToDecimal(textBox2Text.Trim()) + Convert.ToDecimal(textBox1Text.Trim())).ToString();
}
APPROACH 2
If you must have the TextChanged event then you can get textbox values as in code snippet below.
I have commented the call to calcualteA method since the same calculation can be done in TextChanged event. Note how the current grid row is obtained by getting the NamingContainer property of the textbox that raised the TextChanged event.
Get Textbox values in TextChanged event
protected void txt_BCICU_TextChanged(object sender, EventArgs e)
{
//find this textbox Text i.e. txt_BCICU Text
string txtBCICUText = (sender as TextBox).Text;
//find the current grid row and through it other textboxes text
GridViewRow currentRow = (sender as TextBox).NamingContainer as GridViewRow;
//find textbox txt_BCSupDlx Text
string txtBCSupDlxText = ((TextBox)currentRow.FindControl("txt_BCSupDlx")).Text;
//do your calculation here
txt_TotalChargeA.Text = (Convert.ToDecimal(txtBCSupDlxText.Trim()) + Convert.ToDecimal(txtBCICUText.Trim())).ToString();
//calculationA();
}
I have 2 gridviews which update some data, they are independent each one,
both are using "onrowupdating="actualizar"
i want to use the same event like below:
protected void actualizar(object sender, GridViewUpdateEventArgs
e)
{ }
How can i select the specific grid view inside the event?
The object sender will be the GridView who started the event so you can cast it to GridView and you will have it's properties like its Id.
Example:
(sender as GridView).ID
Thats the answer !!
Control cntrl = sender as GridView;
var gridId = cntrl.ID;
I have a dropdownlist control and a button in asp.net page. The dropdownlist is populated from a method. If I select any item other than the first item, after clicking the button, I lose the selected item in the DDL and it selects the first item and also I am getting the value of the first item only in the button click event. How can I fix the problem?
<asp:DropDownList ID="userDropDown" runat="server" DataTextField="CustomerName" DataValueField="CustomerId">
</asp:DropDownList>
protected void Button1_Click(object sender, EventArgs e)
{
if(!page.isPostBack)
{
userDropDown.DataSource = CC.GetCustomers();
userDropDown.DataBind();
}
}
i think you must have bind userDropDown in Page_Load event without condition
if (!IsPostBack)
Please put dropdown binding part inside if (!IsPostBack) condition then it should work
Please bind dropdownlist values inside the if(!ispostback){} or
after submitting button please bind updated field to dropdownlistname.text
It sounds like you are binding your DropdownList to your datasource at ever request. Instead bind it only if Page.IsPostBack is false like below; (You may not need ObjectDataSource)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//bind your datasource here (something like below)
userDropDown.DataSource = GetCustomers();
userDropDown.DataBind();
}
}
As soon as DataBind() method is called it will lose posted data of that object and the FirstItem will be selected by default.
I have dropdownlist inside a repeater and whenever the selected text is changed i have to show it in a textbox how can i do this??
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList6");
TextBox txt = (TextBox)e.Item.FindControl("TextBox4");
txt.Text = ddl.SelectedItem.Text;
}
First, don't use ItemCreated therefore since it triggered too early in the life-cycle(for the ViewState). You would also have to check for the ItemType first.
Instead use the DropDownLists SelectedIndexChanged event directly:
protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList) sender;
RepeaterItem item = (RepeaterItem) ddl .NamingContainer;
TextBox txt = (TextBox) item.FindControl("TextBox4");
txt.Text = ddl.SelectedItem.Text;
}
you could add appropriate OnSelectedChange (somwthing) event handler to the DropDownList and then when event fired you catch it and do whatever you want , you can do it in both client side or server side .
You will need to use the add a handler to associate each dropdown control with the appropriate event handler. I don't have VS in front of me but it should be something like:
txt.SelectedIndexChanged += new EventHandler(YourMethodName)
I have a Repeater control which contains a CheckBox and a lable control in each row. When a CheckBox is selected. I want to retrieve the text of the Label for the corresponding CheckBox. How to get it?
Add the following to the CheckBox.Checked event handler:
CheckBox checkBox = (CheckBox) sender;
Label label = (Label) checkBox.Parent.FindControl("LabelName");
String labelText = label.Text;
You need to use the ItemCommand event of the Repeater as follows:
protected void rep1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Command")
{
Label lbl = e.Item.FindControl("labelID") as Label;
lbl.Text //
CheckBox chk= e.Item.FindControl("chkId") as CheckBox;
chk.Checked //
}
}
You can get a better idea from this article: ASP.Net Repeater OnItemCommand Event using C#
rowcommand event or rowdatabound event, find the 2 controls values from row in that event and get and use values . for more info http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx