TreeView Control - c#

I am developing an application using C#. I am using RadioButtonlist control inside a TreeView. I am getting collection of items from database. Based on the collection items I need to select the Radiobuttonlist items.
For example from the database i got the Collection in this way: Read(R) Write(W)
based on this colletion i need to set up the user permissions.

If I get your question right you want to bind that radiobutton list based on the items in the database based on the current row item on the grid. If thats the case here is your solution.
Lets say you have a Grid called myGrid, a RadioButtonList called myRadio and a HiddenField called myHidden (this is where you bind the value you have on "R" and "W")
All you have to do is when a RowDataBound Event Occurs then you have to assign the value to myRadio
for Example, you have a RadioButtonList like such
<asp:RadioButtonList ID="myRadio" runat="server">
<asp:ListItem Value="R">Read</asp:ListItem>
<asp:ListItem Value="W">Write</asp:ListItem>
</asp:RadioButtonList>
So your code behind should look like this
protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButtonList rdoAnswer = (RadioButtonList)e.Row.FindControl("myRadio");
HiddenField hdnValue = (HiddenField)e.Row.FindControl("myHidden");
rdoAnswer.SelectedValue = hdnValue.Value;
}
}

Related

RadioButtonList with Repeater

I read a very good article here: (Displaying Data with the DataList and Repeater Controls (C#))
https://www.asp.net/web-forms/overview/data-access/displaying-data-with-the-datalist-and-repeater/displaying-data-with-the-datalist-and-repeater-controls-cs
I was trying to respond to the article author but am unable to add my account through Facebook, Twitter, etc at work to ask my question through the site, so I though I would ask here.
The example is very thorough and easy to follow, but I would like to see a RadioButtonList (say with Gender) x Male Female, showing the DB field value.
This would be a big help to compliment the article content
thx
First you need to add the RadioButtonList to the DataList or Repeater (they both work the same so I will provide only one example) and add the OnItemDataBound event.
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"></asp:RadioButtonList>
</ItemTemplate>
</asp:DataList>
Code behind
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
//find the control with findcontrol and cast back to a radiobuttonlist
RadioButtonList radioButtonList = e.Item.FindControl("RadioButtonList1") as RadioButtonList;
//add some listitems, usually filled from list or database
for (int i = 0; i < 5; i++)
{
radioButtonList.Items.Insert(i, new ListItem("ListItem " + i, i.ToString(), true));
}
//cast the dataitem to the datarowview
DataRowView row = e.Item.DataItem as DataRowView;
//set the correct radiobuttonvalue
radioButtonList.SelectedValue = row["dbValue"].ToString();
}

Selecting respective item in drop down list when edit button is clicked

I am working on ASP.NET C# web application. I have a DropDownList for selecting machines_names that is Databound. And a gridview which displays stored data. Gridview also contains templateField for Edit and Delete Buttons. When Edit Button is clicked the stored values of respective fields must be must be loaded on respective field in web form. The problem is when I click edit button, item in ddl (the default value "--select--") is replaced by current data.
I want the stored ddl item to be selected in the ddl when the edit button is clicked.
For example in ddl there are items such as
--select--
Roller
Heater
sensor
In grid view I have the following
Repair-id machine
1 roller
2 sensor
So when I click edit button of ID 2 in grid view, the ddl must select the third item ie sensor without replacing the top item in ddl.
Here's my code
protected DataTable bindMachineName()
{
DataTable dt = new DataTable();
dc.Company_code = Convert.ToInt32(Session["Company_Code"].ToString());
dt = dpbll.select_machine_name(dc);
ddlMachine.DataSource = dt;
ddlMachine.DataTextField = "machine_name";
ddlMachine.DataValueField = "machine_id";
ddlMachine.DataBind();
ddlMachine.Items.Insert(0, new ListItem("----- Select -----", "-1"));
return dt;
}
protected void btnEdit_Click(object sender, EventArgs e)
{
dc.Company_code = Convert.ToInt32(Session["company_code"].ToString());
Button btn = (Button)sender;
GridViewRow gv = (GridViewRow)btn.NamingContainer;
string repair_id = gv.Cells[0].Text;
ViewState["repair_id"] = repair_id;
ddlMachine.SelectedItem.Text = gv.Cells[1].Text;
}
Nevermind I solved the problem.
ddlMachine.Items.FindByText(gv.Cells[1].Text did work perfectly.
protected void btnEdit_Click(object sender, EventArgs e)
{
dc.Company_code = Convert.ToInt32(Session["company_code"].ToString());
Button btn = (Button)sender;
GridViewRow gv = (GridViewRow)btn.NamingContainer;
string repair_id = gv.Cells[0].Text;
ViewState["repair_id"] = repair_id;
ddlMachine.SelectedIndex=ddlMachine.Items.IndexOf(ddlMachine.Items.FindByText(gv.Cells[1].Text));
}
The problem is when I click edit button, item in ddl (the default
value "--select--") is replaced by current data.
You might want to set the DropDownList's AppendDataBoundItems to true.
<asp:DropDownList runat="server" ID="ddlMachine" AppendDataBoundItems="true">
<asp:ListItem Text="----- Select -----" Value="-1" />
</asp:DropDownList>
The AppendDataBoundItems property allows you to add items to the
ListControl object before data binding occurs. After data binding, the
items collection contains both the items from the data source and the
previously added items.

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.

selected index of DDL is always equal to Zero in asp .net

I have a Program Class, with properties as Id, ProgramName,ShortName and Code, im my app
I have a ASP DDL like
<asp:DropDownList ID="DDLProgram" runat="server"
OnSelectedIndexChanged ="OnDDLProgramChanged" AutoPostBack = "true">
</asp:DropDownList>
My OnDDLProgramChanged method is defined as
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
List<CcProgramEntity> programEntities = GetAllPrograms();
DDLProgram.DataSource = programEntities;
DDLProgram.DataTextField = "Shortname";
DDLProgram.DataValueField = "Id";
//My Problem goes here
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
My list is getting all the records correctly, I have checked it. But whenever I am changing an item in the DDL, the selected index in not changing. The selected index is remaining zero.Therefore, I am not being able to get the code of other items but the 0 index's item.
Can anyone help me in this case?
You are binding the data again in your selectedIndex Change event and it will reset your current SelectedIndex after rebinding. You don't need to rebind the data to your dropdown in SelectedIndex Change Event
It should be like..
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
You have to bind data to the DropDownList on page load method
if (!IsPostBack)
{
DDLProgram.DataSource = programEntities;
DDLProgram.DataTextField = "Shortname";
DDLProgram.DataValueField = "Id";
DDLProgram.DataBind();
}
Otherwise it will bind data everytime and hence clear the selection
Why are you assigning the DataSource in OnDDLProgramChanged, this would be resetting the selection you make.

reset selectedvalue when textbox change

I have a textbox and a dropdownlist
the textbox has
<asp:TextBox ID="TxtInizioPeriodo" runat="server"
ontextchanged="InizioPeriodo_TextChanged" AutoPostBack="true" Width="100"></asp:TextBox>
i want if it changes the dropdownlist return to default value selected
i try to pur in page load this:
SelectDestinazione[i].SelectedValue = "";
but it now works.
how can i do?
thanks
In your code you have mentioned like
Array of Dropdownlist ie.,
SelectDestinazione[i].SelectedValue = "";
in the above code "i" selects dropdownlist object from the array of dropdownlists.
Kindly check that one. If its is the single dropdownlist box then use the below solution.
Copy & paste the below code in the server side during the text content change event.
protected void InizioPeriodo_TextChanged(object sender, EventArgs e)
{
SelectDestinazione.SelectedIndex = -1;
}
I assume SelectDestinazione is your dropdownlist
SelectDestinazione.clearSelection();

Categories

Resources