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

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 10570 Views - Last Post: 23 January 2011 - 02:12 AM Rate Topic: -----

#1 response.redirect  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-January 11

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

Posted 21 January 2011 - 08:58 AM

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_...aspx?selected=0
http://test.com/frm_...aspx?selected=1
http://test.com/frm_...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.



    public partial class test : System.Web.UI.Page
    {
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
             
    
            if (!IsPostBack)
            {
    
                int selected;
                
                if (int.TryParse(Request.QueryString["selected"], out selected))
                    RadioButtonList1.SelectedIndex = selected;
                    RadioButtonList1.DataBind(); 
                     
                     
            }
    
    
    
       
        }
      
     
    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);
             
        }
        
         

}


Is This A Good Question/Topic? 0
  • +

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

#2 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

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

Posted 21 January 2011 - 09:37 AM

I'm still fairly new to ASP.NET but don't you need to set a DataSource to DataBind?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_response.redirect*


Reputation:

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

Posted 21 January 2011 - 10:03 AM

View PostSpeedisaVirus, on 21 January 2011 - 09:37 AM, said:

I'm still fairly new to ASP.NET but don't you need to set a DataSource to DataBind?


without it nothing works. no data is rendered on the page.
Was This Post Helpful? 0

#4 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

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

Posted 21 January 2011 - 11:11 AM

Could you post the ASP code too? Have me curious.
Was This Post Helpful? 0
  • +
  • -

#5 response.redirect  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-January 11

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

Posted 21 January 2011 - 12:04 PM

View Postresponse.redirect, on 21 January 2011 - 08:58 AM, said:

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_...aspx?selected=0
http://test.com/frm_...aspx?selected=1
http://test.com/frm_...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.



    public partial class test : System.Web.UI.Page
    {
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
             
    
            if (!IsPostBack)
            {
    
                int selected;
                
                if (int.TryParse(Request.QueryString["selected"], out selected))
                    RadioButtonList1.SelectedIndex = selected;
                    RadioButtonList1.DataBind(); 
                     
                     
            }
    
    
    
       
        }
      
     
    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);
             
        }
        
         

}


-----------here's the aspx code on the frontend-----------------------

<%@ Page Title="" Language="C#" ViewStateMode="Enabled" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
  
<table width="100%"><tr><td>

<!-- Body -->

<asp:DetailsView ID="DetailsView3" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource2" GridLines="None"><Fields>
 
            
<asp:TemplateField HeaderText="Title" ShowHeader="False" SortExpression="Title">


<EditItemTemplate><asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox2" ErrorMessage="*"></asp:RequiredFieldValidator></EditItemTemplate>


<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Title") %>'>
</asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox2" ErrorMessage="*">
</asp:RequiredFieldValidator>
</InsertItemTemplate>
                

<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
                   
            
<asp:TemplateField HeaderText="ArticleText" ShowHeader="False" SortExpression="ArticleText">

<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ArticleText") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="*"></asp:RequiredFieldValidator>
</EditItemTemplate>
                
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ArticleText") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="*"></asp:RequiredFieldValidator>
</InsertItemTemplate>

<ItemTemplate>
<%# Eval("ArticleText") %>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT [ArticleText], [Title] FROM [test_Article] WHERE ([Article_PK] = @Article_PK)" onselecting="SqlDataSource2_Selecting">
<SelectParameters>

<asp:ControlParameter ControlID="RadioButtonList1" DefaultValue="1" Name="Article_PK" PropertyName="SelectedValue" Type="Int32" ConvertEmptyStringToNull="True" />
        </SelectParameters>
    </asp:SqlDataSource>
    </td>
<td  width="25%" align="left" valign="top">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT Article_PK, ArticleText, Score, Title, Url_FK FROM test_Article WHERE (Url_FK = @URL_FK)" onselecting="SqlDataSource1_Selecting">

<SelectParameters>
<asp:Parameter DefaultValue="0" Name="Url_FK" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>


<asp:RadioButtonList ID="RadioButtonList1" onselectedIndexChanged="RadioButtonList1_SelectedIndexChanged" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Title" DataValueField="Article_PK">
</asp:RadioButtonList>
                                        
                                        
<br />
  
                                    </td></tr>
<tr><td valign="top" class="style2"></td><td class="style2">
    &nbsp;</td><td align="left" class="style2"></td></tr>
<tr><td valign="top"></td><td>&nbsp;</td><td align="left"></td></tr>
<tr><td valign="top"></td><td></td><td align="left"></td></tr>
</table> 
</asp:Content>

Was This Post Helpful? 0
  • +
  • -

#6 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

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

Posted 21 January 2011 - 06:54 PM

from what I can see here, without being able to run it, it looks like the datasource used by your radiobuttonlist requires one parameter in its select statement. That parameter is set from Session["URL_PK"] during the onselecting event of the datasource, otherwise it defaults to 0. My question to you is when/where does Session["URL_PK"] get it's value set?

also if you're getting a parameter's value from session there is a sessionparameter object you can use.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT Article_PK, ArticleText, Score, Title, Url_FK FROM test_Article WHERE (Url_FK = @URL_FK)" onselecting="SqlDataSource1_Selecting">  

<SelectParameters>  
    <asp:SessionParameter DefaultValue="0" Name="Url_FK" SessionField="URL_PK" type="Int32" />  
</SelectParameters>  

</asp:SqlDataSource>  




Then you won't need to set it in the onselecting event.

Edit:
In the datasource configuration wizard, Session is available in the Source drop down when you click on the "WHERE" button.

This post has been edited by Nakor: 21 January 2011 - 06:58 PM

Was This Post Helpful? 1
  • +
  • -

#7 response.redirect  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-January 11

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

Posted 21 January 2011 - 07:45 PM

don't worry about the url_fk, that's just to read in records that have be created for a particular website. my database supports mulitple websites.
Was This Post Helpful? 0
  • +
  • -

#8 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

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

Posted 21 January 2011 - 08:38 PM

try databinding your radio button list before you set its selected index. and since you're setting your selected index in the code behind i don't believe it will call the selectedIndexChanged event of the radio button list so you will probably need to set the text area in the page load as well as the selected index of the radio button list or you could make a call to the selectedindexchanged method from page load.

RadioButtonList1_SelectedIndexChanged(RadioButtonList1.SelectedItem, new EventArgs());



just making a simple mock of what you're doing, this is how my page load looks that got it working in the way you described.

                protected void Page_Load(object sender, EventArgs e)
		{
			List<ListItem> items = new List<ListItem>()
				{
					new ListItem(){Text="Item 1"},
					new ListItem(){Text="Item 2"},
					new ListItem(){Text="Item 3"},
					new ListItem(){Text="Item 4"}
				};

			RadioButtonList1.DataSource = items;
			RadioButtonList1.DataBind();

			if (!IsPostBack)
			{
				int selected;

				if (Request.QueryString["selected"] != null)
				{
					if (int.TryParse(Request.QueryString["selected"], out selected))
						RadioButtonList1.SelectedIndex = selected;                    
				}
				else
				{
					RadioButtonList1.SelectedIndex = 0;
				}

				button_change(RadioButtonList1.SelectedItem, new EventArgs());
			}
		}



with this the default selected index for the radio buttons is 0. If the "selected" query string exists then we set the selected index to that otherwise we use the default value. Then i call the radiobuttonlists selected index changed event to set the text of the text area.

This post has been edited by Nakor: 21 January 2011 - 09:04 PM

Was This Post Helpful? 0
  • +
  • -

#9 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

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

Posted 21 January 2011 - 09:04 PM

Now, when i put "?selected=number" at the end of the url it changes the radio buttons accordingly and set the text of the text area. Also, if you want to avoid a potential index out of range error you really ought to make sure that the number brought in through the query isn't larger than the number of items in your radio button list, otherwise you could be setting the selected index to a value that doesn't exist.

This post has been edited by Nakor: 21 January 2011 - 09:09 PM

Was This Post Helpful? 0
  • +
  • -

#10 response.redirect  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-January 11

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

Posted 22 January 2011 - 03:07 AM

I'm having a hard time getting this to work. my radiobuttonlist and textarea pull from a database so i'm assuming i don't need to the list items portion of your code. therefore my code behind should look like the following below, correct?? when i implement this my redirect throws errors.

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            int selected;

            if (Request.QueryString["selected"] != null)
            {

                if (int.TryParse(Request.QueryString["selected"], out selected))

                    RadioButtonList1.SelectedIndex = selected;
            }

            else
            {

                RadioButtonList1.SelectedIndex = 0;

            }

           RadioButtonList1_SelectedIndexChanged(RadioButtonList1.SelectedItem, new EventArgs());

        }

    } 
        
        
  
     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 = "frm_Articles.aspx?selected=" + RadioButtonList1.SelectedItem;
            Response.Redirect(strRedirect);
                
}     }

Was This Post Helpful? 0
  • +
  • -

#11 response.redirect  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-January 11

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

Posted 22 January 2011 - 03:20 AM

does it work when you open a new browser and point and past in a test.com?select=[any number] url? i've only been able to point to a radio value via a test.com?select[any number] url if am within the same browser i launch my webite in or if i open a new tab and past the url in. once i launch a new browser the page come up blank. i'm using IE btw.
Was This Post Helpful? 0
  • +
  • -

#12 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

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

Posted 22 January 2011 - 06:21 AM

Right, you wouldn't need the list, I was just using that as a dummy datasource since I don't have the data you have. Can you post the error that you're getting?

This post has been edited by Nakor: 22 January 2011 - 06:23 AM

Was This Post Helpful? 0
  • +
  • -

#13 response.redirect  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-January 11

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

Posted 22 January 2011 - 07:00 AM

the redirect just gets stuck in an infinite loop and the page doesn't render. the status bar the bottom of the page just show the http request looping.

i've tried using "selectedItem" and "selectedIndex"
RadioButtonList1_SelectedIndexChanged(RadioButtonList1.SelectedItem, new EventArgs());

Was This Post Helpful? 0
  • +
  • -

#14 response.redirect  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-January 11

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

Posted 22 January 2011 - 07:14 AM

also note that my orginal code works when i create a link within another page:
<a href="http://test.com/test.aspx?selected=2">test link</a>

must have something to do with the postback logic then, correct? if it can read in the radio button value on when i create a postback but if i just cut and paste the link into another browser it doesn't read the value.
Was This Post Helpful? 0
  • +
  • -

#15 response.redirect  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-January 11

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

Posted 22 January 2011 - 11:41 AM

when postback is false, the code doesn't seem to be setting the value for RadioButtonList1.SelectedIndex. it remains a -1 when i step through the code in debug mode.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2