how to display selected value from radiobuttonlist - asp.net

I tried
Dim theme = rblTheme.Items(rblTheme.SelectedIndex).Value
getting first value selected
Dim theme = rblTheme.SelectedItem.Value
getting first value selected
Dim theme = ""
For i As Integer = 0 To rblTheme.Items.Count - 1
If (rblTheme.Items(i).Selected) Then
theme = rblTheme.Items(i).Value
End If
Next
Getting both value selected.
Need help !!

Dim selectedItemVal as string
selectedItemVal=rblTheme.SelectedValue;
I guess you are reloading the RadioButton list in the page postback again (when you click on some button). So probably you should move that code If isPostBack property is false
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Your code to load the Radio button list control
}
}
Now you should be able to get the value on your button click event.

Related

How to gain row value of button clicked in a radgrid's DetailTable

If I were to want to gain the value of a row in the master table in a template column button I would do:
" OnCommand=event>
Event
GridDataItem item = (GridDataItem)RadGrid1.Items[Convert.ToInt32(e.CommandArgumrnt)];
I am not sure what index the grid returns when the detailtable button is clicked and every time I try to access a column in that row I get an error saying column does not exist. I tried doing so:
GridDataItem item = (GridDataItem)RadGrid1.MasterTable.DetailTables[0].Items[Convert.ToInt32(e.CommandArgumrnt)];
This did not work as well.
Excuse me if I made any mistakes, I am on my mobile phone.
I have found a much simpler solution to my problem that doesn't if require me to use oncommand or command argument! Instead I use the simple Onclick.
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridDataItem item = (GridDataItem)btn.NamingContainer;
TableCell cell = (TableCell)item["UniqueName"];
string value = cell.Text;
}

Getting wrong sender in event handler for checkboxes

I have dynamically generated checkboxes and having EventHandler which takes care about check change of any of the checkbox. The issue is that if i uncheck any of the checkbox, it gets all other checkbox as sender and checkchange is called according to number of checkboxes number of times. If there are 3 checkboxes and 1 is unchecked than checkchange event handler is called 2 times. I dont understand what is happening.
CheckBox chkbox;
panelDynamicCheckbox.Controls.Clear();
foreach(string product in products)
{
chkbox = new CheckBox();
chkbox.ID = product;
chkbox.Text = product;
chkbox.AutoPostBack = true;
chkbox.CheckedChanged += new EventHandler(this.CheckChanged);
panelDynamicCheckbox.Controls.Add(chkbox);
}
protected void CheckChanged(object sender, EventArgs e)
{
CheckBox checkbox = (CheckBox)sender;
}
Ensure the code to add the dynamic checkbox and especially the event handler specification is done in the OnInit method in the page life-cycle. If you don't do this, these sort of unpredictable state behavior can be seen.

access lable control kept in layout of listview?

I have one dropdownlist and one button and listview. Button and dropdownlist is outside listview.
On 1st page load, all the item is displayed. Now user can see based on selection of dropdownlist.
I want to show selected text of dropdownlist in lable of layout control of listview.
I wrote below code but doesn't seem to work correctly.
protected void lvProduct_LayoutCreated(object sender, EventArgs e)
{
if (dlCategory.SelectedIndex != -1)
{
string Category= dlCategory.SelectedItem.Text.ToString();
(lvProduct.FindControl("lblCategory") as Label).Text = "Category: " + dlCategory.SelectedItem.Text.ToString();
}
else
{
(lvProduct.FindControl("lblCategory") as Label).Text = "All category";
}
}

Find a GridView row programmatically

The row has a LinkButton that when clicked needs to highlight the row.
Code so far:
protected void linkbutton1_Click(object sender, EventArgs e)
{
LinkButton l = (LinkButton)sender;
GridViewRow g = (GridViewRow)l.Parent; // what is the correct way to do this?
//g.Style etc etc
}
first of all set the "CommandName" property of LinkButton to "select",
then in the selectedIndexChanging event of gridview write below code:
for (int i = 0; i < GridView1.Rows.Count;i++ )
GridView1.Rows[i].BackColor = System.Drawing.Color.White;
GridView1.Rows[e.NewSelectedIndex].BackColor = System.Drawing.Color.Cornsilk;
Make use of the RowCommand event of the GridView instead of the Click event of the LinkButton.
Then you can have a CommandName on the LinkButton such as "HighlightRow" and do something like the following:
Select Case e.CommandName
Case "HighlightRow"
e.item.row.attributes("class") = "highlight"
End Select
Sorry its in VB.NET and not C#
1.) Set Command Name Property to "Select"
2.) Change style either on code behind as shown by #Raymond or give set Cssclass attribute for
SelectedRowStyle of gridview to CssClass="selecterowstyle"
.selectedRowstyle
{
background-color:#EAEAEA;
}

How to use the FindControl function to find a dynamically generated control?

I have a PlaceHolder control inside of a ListView that I am using to render controls from my code behind. The code below adds the controls:
TextBox tb = new TextBox();
tb.Text = quest.Value;
tb.ID = quest.ShortName.Replace(" ", "");
((PlaceHolder)e.Item.FindControl("ph_QuestionInput")).Controls.Add(tb);
I am using the following code to retrieve the values that have been entered into the TextBox:
foreach (ListViewDataItem di in lv_Questions.Items)
{
int QuestionId = Convert.ToInt32(((HiddenField)di.FindControl("hf_QuestionId")).Value);
Question quest = dc.Questions.Single(q => q.QuestionId == QuestionId);
TextBox tb = ((TextBox)di.FindControl(quest.ShortName.Replace(" ","")));
//tb is always null!
}
But it never finds the control. I've looked at the source code for the page and the control i want has the id:
ctl00_cphContentMiddle_lv_Questions_ctrl0_Numberofacres
For some reason when I look at the controls in the ListViewDataItem it has the ClientID:
ctl00_cphContentMiddle_lv_Questions_ctrl0_ctl00
Why would it be changing Numberofacres to ctl00? Is there any way to work around this?
UPDATE:
Just to clarify, I am databinding my ListView in the Page_Init event. I then create the controls in the ItemBound event for my ListView. But based on what #Womp and MSDN are saying the controls won't actually be created until after the Load event (which is after the Page_Init event) and therefore are not in ViewState? Does this sound correct?
If so am I just SOL when it comes to retrieving the values in my dynamic controls from my OnClick event?
UPDATE 2:
So i changed the code i had in my Page_Init event from:
protected void Page_Init(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//databind lv_Questions
}
}
to:
protected void Page_Init(object sender, EventArgs e)
{
//databind lv_Questions
}
And it fixed my problem. Still a little confused as to why I want to databind regardless of whether it's a postback or not but the issue is resolved.
It looks like you're adding your textbox to a Placeholder control... but then you're searching a ListViewDataItem container for it later.
Seems to me that you need to search for the Placeholder first, and then search it for the textbox.

Resources