Monday, March 26, 2012

Setting Selected RadioButton From Database Value

I'm having trouble assigning which one of three radiobuttons in a group is checked depending on the value received from a database.

I have a form which is used for editing details of users os a site. The form is filled each time the OnSelectedIndexChanged method of a dropdownlist to trigger a sub routine.

As part of that routine a database is queried and various values are retreived. One of these is the level of access that the user is permitted. This is stored as an integer where 1=manager, 2=standard user and 3=read only.

On my form I have three radiobuttons marked as Manager, Standard and Read Only. I want the appropriate button to be checked when the relevant data is retreived. At the checked button does not change.

I've found many articles which tell you how to assign a value to something depending on whcih radiobutton is checked, but none that change which button is checked depending on a particular value.

Here are the relevant parts of my code.


if dsUser.Tables("Users").Rows(0).Item("ACCESSLEVEL")=1 then
radMan.Checked(True)
else if dsUser.Tables("Users").Rows(0).Item("ACCESSLEVEL")=2 then
radStan.Checked=True
else if dsUser.Tables("Users").Rows(0).Item("ACCESSLEVEL")=3 then
radRead.Checked=True
end if

This is the code within the html


<td><div align="center"><asp:radiobutton id="radMan" Text="Manager" GroupName="radLevel" runat="server" AutoPostBack="true" />
</div></td>
<td><div align="center"><asp:radiobutton id="radStan" Text="Standard" GroupName="radLevel" runat="server" AutoPostBack="true"/>
</div></td>
<td><div align="center"><asp:radiobutton id="radRead" Text="Read Only" GroupName="radLevel" runat="server" AutoPostBack="true"/>
</div></td>

I have tried with AutopostBack=True and with this left out.

Thanks
JasonWhere in your code are you trying to bind the value to the RadioButton? Are you using the DataBind() method during PostBack or Page_Load?
Hi,

How much is setting AutoPostBack = true for your radiobuttons going to help you. First remove the AutoPostBack= true.

In your case, you just want the radiobutton to be checked depending on the Access Level right?

Hmm... Try putting the logic which you use within the If Not IsPostBack Then... End If codeblock.

Write back it it doesnt help.
Thanks for your reply. It is the simple answer that I'm after (they're the ones I understand, sometimes).

I've taken out the AutoPostback=true, as I mentioned in my original post I was just trying various options as I couldn't figure out why it wasn't working.

I've put my logic withint the If Not PostBack then ...end if block as you suggest, but still nothing happens.

In the documentation that I found on RadioButton it said that


myObject.Checked

would get or set which button was checked. Whenever I try this I get an error saying that "Property access must asign to the value or use it's value".

The "=true" part is something I added to try and stop these errors. It did stop the errors but the I still can't get the relevant button checked.

Thanks
Jason

0 comments:

Post a Comment