Catch dropdown selected text inside a repeater - c#

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)

Related

How to get value of TextBox of GridView in C#?

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();
}

Checkboxlist loop is not working

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.

Building dynamic DropDownList with Event Handling

I want to build a dynamic DropDownList and add some list item.
With below code I can do it.
protected void BuildDynamicDropDown()
{
DropDownList ddl = new DropDownList();
ddl.ID = "newDDL";
ddl.SelectedIndexChanged += dynamicDDL_SelectedIndexChanged;
ddl.Items.Add(new ListItem("stack1", "stack1"));
ddl.Items.Add(new ListItem("stack2", "stack2"));
ddl.Items.Add(new ListItem("stack3", "stack3"));
ddl.AutoPostBack = true;
Panel1.Controls.Add(ddl);
}
protected void dynamicDDL_SelectedIndexChanged(object sender, EventArgs e)
{
//this part of code should trig another dynamic dropdown
}
But I want to use that SelectedIndexChanged event in order to change another dynamic dropdown value.
Do you have any idea?
At this link how to create event handler for dynamic drop down list in c#
one solution likes below
ddlFilter.SelectedIndexChanged += new EventHandler(ddl2_SelectedIndexChanged);
ddlFilter.AutoPostBack = true;
void ddlFilter_SelectedIndexChanged(object sender, EventArgs e)
{
//your code
}
Shortly,
I want to build more than one dynamic dropdown and assume that 3 dropdown and I want to handle their selectedindexchange events in order to interact between themselves.
If i m getting your Query!!
You can use Items array of dropdownlist and loop through it and check the condition and do the changes in the items list of another dropdownlists on selected index changed.

Finding a control on Dropdownlist SelectedIndexChanged in Repeater

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;
}

How to find a label in a asp repeater

Structure of my asp: repeater
repeater
updatePanel
label1 (rating)
button (updates rating)
some_picture (thing being rated)
/update panel
/repeater
Imagine the output of the above repeater containing 100 rows. (1 label, and 1 button on each row).
Goal: when I click the button, I want the appropriate label to be updated. I dont know how to do this.
I can reference a label via:
Label myLabel2Update = (Label)Repeater1.Controls[0].Controls[0].FindControl("Label1");
But ofcourse, it will be the same label each time (not necessarily the label that needs to be updated). I need to update the label that is on the same row as the button.
Any guidance would be appreciated.
Handle the ItemCommand event of the repeater. In your event handler check the Item property of the event arguments and use findcontrol on that. e.g.
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Label Label1 = (Label)e.Item.FindControl("Label1");
}
Label1 will be the label in the same item as the button that was clicked.
Or in response to Dr. Wily's Apprentice's comment you could do the following
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case "bClick":
Label Label1 = (Label)e.Item.FindControl("Label1");
/*do whatever processing here*/
break;
}
}
And then for each button specify the command name "bClick"
You will need a helper method to iterate through the hierarchy or use the
Control's FindControl(string id) method for that.
Example:
var stateLabel = (Label)e.Row.FindControl("_courseStateLabel");
I assume there's an event handler for the button? If so, you should be able to do
protected virtual void OnClick(object sender, EventArgs e)
{
var label = ((WebControl)clickedButton).Parent.FindControl("Label1");
}

Categories

Resources