linking to a radiobutton value, data is not binding, asp.net c# - 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.

Related

Remove Entry Text at first click

I have a entry/Textbox. And i have 10 number buttons as a calculator.(0-1-2-3-4....) When my page loaded entry having a number. Its showing me start value.( For example when my page loaded entry text being 10 already without any click to button) So i want to when i click a number button It will delete first entry value that loaded with page and it will start write my button clicks. How can I do it ?
I tried with:
private void btn1_Clicked(object sender, EventArgs e)
entry.Text="";
entry.Text="1";
But of course it didn't work because if i want to write 123 I cant write its always deleting. I just want delete first value which is coming with page loaded.
As #Jason suggested use the Placeholder property to display the initial value and in the click events append the value to the entry text:
Note: you can have a single event delegate that handle the click event for all buttons:
Constructor()
{
//set initial value here or use binding
//entry.Placeholder =
}
private void btn_Clicked(object sender, EventArgs e)
{
//you can remove the initial value if you want
//entry.Placeholder = string.Empty;
string buttonText = ((Button)sender).Text;
entry.Text = $"{entry.Text}{buttonText}";
}
An alternative way is to add a boolean flag out of the method to avoid this.
public class MyCalculator ()
{
private bool _isNewNumber;
protected override void OnAppearing()
{
_isNewNumber = true; //whenever open the page, needs to refresh
}
private void btn1_Clicked(object sender, EventArgs e)
{
if (_isNewNumber)
{
entry.Text=""; //clear the number for the first time
}
entry.Text="1";
_isNewNumber = false;
}
}

Testing to verify that a button is clicked first before another is clicked "Webfomrs"

I used an if statement saying that if lblTotalAmount is populated then you can be able to click the second button. Because if lbltotalamount is populated then the first button was clicked to populate it. However, with my code below it works by showing the error message if you try to click the second button before the first button but then if i do it in the correct order it will not redirect me to the page i stated below. How can i correctly state this so that it will work?
protected void btnSubmitOrder_Click(object sender, EventArgs e)
{
if (lblTotalAmount == null)
{
Response.Redirect("~/Default.aspx");
}
else
{
lblMessage.Text = "Please click the Calculate Order Total button first";
}
}
You should be trying to validate on the Text property of the lablel instead
protected void btnSubmitOrder_Click(object sender, EventArgs e) {
if (string.IsNullOrEmpty(lblTotalAmount.Text))
{
Response.Redirect("~/Default.aspx");
}
else
{
lblMessage.Text = "Please click the Calculate Order Total button first";
}
}

Adding radiogroup, buttons and controls in asp.net using 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.

C# / asp.net global Variables?

I have two buttons with on click functions
The 1st one gets assigned a variable when Clicked.
How do I get my second button to get the variable from the 1st button when I click button 2?
It doesn't seem to work. As the second button doesn't recognise the Variable.
Thanks
EDIT:
Just to clarify My code is generating a pdf. button 1 selects the url of the template to use. and in button 2 (the one generating the pdf) I want it to get the variable set from button 1 so it knows what template to use.
EDIT 2:
My code does work but only when I'm not using the ajax update panel. it seems that the variable I'm trying to set doesn't get set with AJAX
Your Button have Id, you get this button with his Id
Nota : You can add runat="server" in order to visualize in server side
<asp:Button id="Button1"
Text="Click "
OnClick="Btn1_Click"
runat="server"/>
<asp:Button id="Button2"
Text="Click "
OnClick="Btn2_Click"
runat="server"/>
void Btn2_Click(Object sender, EventArgs e)
{
Button1.Text = "test after click on button 2";
Template = ...;//Set your value
}
void Btn1_Click(Object sender, EventArgs e)
{
Button2.Text = "test after click on button 1";
//Here you can get your value after post.
var result = Template;
}
It's not subject but in delegate you can also get objet button by passing on sender argument.
var button = sender as Button; //You get button who raise event
In order to manage Template Path property.
public string Template
{
get
{
if(ViewState["Template"] != null)
{
return (string)ViewState["Template"];
}
}
set{ViewState["Template"] = value;}
}
i guess you are looking at accessing value of a variable inside the click event of button2 for which the value is set in the button1 click event ?
private string myPrivateString = "";
void Page_Load()//Not sure of correct method signature
{
if(Page.IsPostBack)
{
myPrivateString = Session["myPrivateString"];
}
}
void Button1_Click(object sender, EventArgs e)
{
//There will a postback before this gets executed
myPrivateString = "Value Set From Button 1";
Session["myPrivateString"] = myPrivateString;
}
void Button2_Click(object sender, EventArgs e)
{
//There will a postback before this gets executed
//Accessing myPrivateString here without setting value from session
//will return empty string as after PostBack its a new page thats rendered.
myPrivateString = Session["myPrivateString"]; // Or do it in the Page_Load event
}
I guess now you can get the value of inside the button2 click event.
Also read about ASP.NET Page lifecycle and how client side events like button clicks are handled by the ASP.NET framework.

Page lifecycle question

I am using a DropDown menu and an UpdatePanel to filter out a DataGrid. The DataGrid has buttons which redirect to a different page. When I hit the back button or a link on top of the other page, it redirects me to the page with the DropDown as it should...but it gets rid of the DataGrid data and I have to make a selection from the DropDown again. Is there a way to make sure that the DropDown selection is remembered when both the link is pressed and the back button selected? Thanks for your help!
The easiest thing to do in this case is to save the dropdown selection in Session collection
and on page load, check to see if there is a saved selection and use it to reapply the selection.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Session["SavedSelection"] = DropDownList1.SelectedIndex;
}
protected void Page_Load(object sender, EventArgs e)
{
if(Session["SavedSelection"] != null)
{
int selectedIndex = (int) Session["SavedSelection"];
DropDownList1.SelectedIndex = selectedIndex;
}
}

Categories

Resources