Adding radiogroup, buttons and controls in asp.net using c# - c#

Here is the complete code. I want to display a radiogroup when I select 1 in the dropdown list box. I get the error 'System.Web.HttpException: Control 'RadioButton1' of type 'RadioButton' must be placed inside a form tag with runat=server'.
namespace HostelRoomManagement
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "1")
{
RadioButton rb1 = new RadioButton();
rb1.ID = "RadioButton1";
rb1.Text = "C block";
rb1.GroupName = "BlockGroup";
RadioButton rb2 = new RadioButton();
rb2.ID = "RadioButton2";
rb2.Text = "C block";
rb2.GroupName = "BlockGroup";
Page.Controls.Add(rb1);
Page.Controls.Add(rb2);
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
}
}
}
I get the error 'System.Web.HttpException: Control 'RadioButton1' of type 'RadioButton' must be placed inside a form tag with runat=server'

You can add the radio buttons you want the user to see based on his choice to the page and set the Visibility of them to false. Then, once the user choose a value change the visibility of the radio buttons you want to true. It might be easier.

I suspect your problem is that you are referencing to SelectedValue whereas you want to be refering to selectedindex.
Hope this helps.

You have created two radio button but where have you added them on the page?
Start by creating a place holder for you radio-button lists and add these controls over there.
The dynamically created control will be lost on the post back. This means you will have to manage you dynamically created controls.
Here is a good [example]: 1 ASP.NET dynamically created controls and Postback.

Related

Event that would listen to any label click in form

Well, I'm making a chess game that's based on labels. I need to listen for label click, so when user clicks on an label, I get the name of label he clicked. I know I can do it for each label, but is there an universal event that would help me do the same thing for all of them in one event / loop?
Suppose you have taken 64 labels.
In windows form, on click event of Label1 you will write following code:
private void Label1_Click(object sender, EventArgs e)
{
var label = sender as Label;
MessageBox.Show(label.Name);
}
For remaining 63 Lables, In Design view, select all 63 lables by using Ctrl key --> Go to Property window --> Under Event option select Click option --> From dropdownlist select 'Label1_Click' option.
Just finish & run the application.
You can use a Panel
structure to group your labels and then call the desired event on that Panel, so it will trigger whenever you click one of it's elements.
Another solution would be to identify your label with the coordinates of the mouse click (the amount of code that requires depends how you placed them of course).
Like mentioned in the comments you can assign one event to all...
List<Label> lbls = this.Controls.OfType<Label>().ToList();
foreach (var lbl in lbls)
{
lbl.Click += lbl_Click;
}
void lbl_Click(object sender, EventArgs e)
{
Label lbl = sender as Label;
MessageBox.Show(lbl.Name);
}
You can assign these methods to every label you need to manage in the VS form designer (you go to events of controls, at the click line and select the method in the list instead of double click on it):
private void Label_Click(object sender, EventArgs e)
{
var nameLabel = ( sender as Label )?.Name ?? "Error";
// ...
}
private void Label_Enter(object sender, EventArgs e)
{
( sender as Label ).Cursor = Cursors.Hand;
}
private void Label_Leave(object sender, EventArgs e)
{
( sender as Label ).Cursor = Cursors.Default;
}
Cursor change added for convenience if you want.
If you want to dynamically assign events, you can use the #caner answer, and you can group all in a panel to parse Panel.Controls and assign event.
You can create a custom label class that inherits from Label. You can then subscribe to the Click event of the base class and do your thing.
public class MyLabel : Label
{
public MyLabel()
: base()
{
Click += ProcessClickEvent;
}
private void ProcessClickEvent(object sender, System.EventArgs e)
{
// Do what you want to do
}
}

Getting two new tabs on one click of button in tab control

Hello everyone I am trying to add new tab in in my winform I am using the following code in my form. But when I click on the button its add two tabs instead of one tab
Please review my code where I am going wrong
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
TabPage tab = new TabPage("New Tab");
tabControl1.TabPages.Insert(tabControl1.TabPages.Count - 1,tab);
tabControl1.SelectedTab = tab;
}
This happen because you assign to tabControl1.Selected property the newly created page. The assignment raises again the SelectedIndexChanged and you add another tabpage.
You could prevent this recursion adding a global class level variable to your form and using it as a semaphore before entering your adding page code
private bool addingPage = false;
....
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if(!addingPage)
{
addingPage = true;
TabPage tab = new TabPage("New Tab");
tabControl1.TabPages.Insert(tabControl1.TabPages.Count - 1,tab);
tabControl1.SelectedTab = tab;
addingPage = false;
}
}

button action not firing

I have a web form which dynamically loads controls upon selection in combobox(devexpress). I have the following code on main form
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
if (Session["_active_control"] != null)//persist control on postbacks
{
Control cntrl = Session["_active_control"] as Control;
pnl_main.Controls.Clear();
pnl_main.Controls.Add(cntrl);
}
}
protected void cmb_control_SelectedIndexChanged(object sender, EventArgs e)
{
Control cntrl= Page.LoadControl("~/" + cmb_control.SelectedItem.Value);
pnl_main.Controls.Clear();
pnl_main.Controls.Add(cntrl);
Session["_active_control"] = cntrl;
}
also I have a user control having three Textboxes and a button having code
protected void btn_save_Click(object sender, EventArgs e)
{
lbl.Text = ASPxTextBox1.Text + "<br>" + ASPxTextBox2.Text + "<br>" + ASPxTextBox3.Text;
}
My problem is that the save button of user control is not firing if i load it dynamically (I have checked using breakpoints and also the code shown above. however it runs smoothly if I use it statically.(i.e. by dragging in design mode)
You are right that you have to persist the control across postbacks.
However the Page Load event is too late to add back your controls. Do this on the Init event of your page and you should be good. To receive a postback event, the control should be present when ProcessPostData(called before PreLoad) is called.
Also for textboxes you will want to receive the values entered by the user. This too happens on ProcessPostData, if you add you control after that, you will not receive the values entered by the user.
Refer: ASP.NET Page Life Cycle
hey i found the solution
instead on creating the controls in combobox_selectedindexchanged i put my control creation code on Pageload based in combobox.selectedindex i.e.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (cmb_control.SelectedItem != null)
{
Control cntrl = Page.LoadControl("~/" + cmb_control.SelectedItem.Value);
cntrl.ID = "_new_ctrl" + cmb_control.SelectedItem.Value;
pnl_main.Controls.Clear();
pnl_main.Controls.Add(cntrl);
}
}
see Button click event not firing within use control in ASP .Net

C# web form listbox OnSelectedIndexChanged

I have a listbox on my website, which contains some elements.
I made the event, OnSelectedIndexChanged, so when the user presses an element, this value will be put into a textbox
protected void Page_Load(object sender, EventArgs e)
{
listbox = new Listbox();
// Add to page etc.
listbox.SelectedIndexChanged += new EventHandler(listbox_SelectedIndexChanged);
}
void listbox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
textbox_name.Text = listbox.SelectedItem.ToString();
}
catch
{
textbox_info.Text = "Choose employee";
}
}
It works in c# windows forms, but not in web forms for some reason.
Is it possible to get it to work?
Thank you
Set autopostback property of the listbox true

linking to a radiobutton value, data is not binding, asp.net c#

hi i need to modify my code a little bit. i have a page with a radio button list and a textarea. the textarea is populated when a users makes a radio button selection.
also, when a user makes a radio button selection the url will hold an extention in the url to show which selection index number they have selection. (i.e. ?selected=0)
http://test.com/frm_Articles.aspx?selected=0
http://test.com/frm_Articles.aspx?selected=1
http://test.com/frm_Articles.aspx?selected=2
that way they can copy the url and reference it in other websites as a link. or place it in their favorites.
the problem is, if you grab the url and open a new browser, the page does not pass the value and databind accordingly. no radio buttons or content appear on the page.
must be the postback logic i think???
what's working:
when i launch the website the radio buttons appear and index 0 is set
when i select radio buttons the correct data displays and urls linking to radio button values display in browser (i.e. http://test.com/test.aspx?selected=2)
if i cut and paste pointer urls within the same browser then correct data is rendered
what doesn't work (everything that deal with an false PostBack):
1.when i launch website no data within the textarea apprears even though the radio button is set to 0 index and is visiable.
2. if i cut and paste pointer url into a new browser, text area and radio buttons do not display.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
int selected;
if (Request.QueryString["selected"] != null)
{
if (int.TryParse(Request.QueryString["selected"], out selected))
{
RadioButtonList1.SelectedIndex = selected;
RadioButtonList1.DataBind();
}
}
else
{
int firstart = 0;
RadioButtonList1.SelectedIndex = firstart;
RadioButtonList1.DataBind();
}
}
}
protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
//
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
try{
e.Command.Parameters["#URL_FK"].Value = Session["URL_PK"];
}
catch (Exception ex)
{
}
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strRedirect;
strRedirect = "test.aspx?selected=" + RadioButtonList1.SelectedIndex;
Response.Redirect(strRedirect);
}
}
In your code at Page_Load event before this line
RadioButtonList1.SelectedIndex = selected;
you should bind RadioButtonList1. after binding RadioButtonList you can set SelectedIndex.
my SqlDataSource1_Selecting method was the issue. i used another approach and my code worked.

Categories

Resources