By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,982 Members | 1,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,982 IT Pros & Developers. It's quick & easy.

fire event handler for each radiobutton

P: n/a
I have a Asp.net table control that I dynamically add rows from a SQL
database. Inside the table, I add radiobuttons and they all have
different ID numbers according to BindChain()

Looking In BindChain() Sub for the line:
AddHandler Rb.CheckedChanged, AddressOf BindAssembly_Click

I can't get the radiobutton to attach the same BindAssembly_Click()
event for each radiobutton. In fact it is attaching an event with the
ID number and the page gets posted when I click on it. It's weird. I
don't know where it's getting that from. I am desperate, Please help
me.

Table HTML code generated
<table id="Tbchain" border="0"
style="border-width:1px;border-style:solid;">

<tr>

<td><input id="Radio0" type="radio" name="MySel" value="Radio0"
checked="checked" onclick="__doPostBack('Radio0','')"
language="javascript" /><label
for="Radio0">C188</label></td><td></td><td><img
src=chain_images/C188.gif></td><td>&nbsp;</td>

</tr><tr>

<td><input id="Radio1" type="radio" name="MySel" value="Radio1"
onclick="__doPostBack('Radio1','')" language="javascript" /><label
for="Radio1">D667</label></td><td></td><td><img
src=chain_images/D667.gif></td><td>&nbsp;</td>

</tr><tr>

<td><input id="Radio2" type="radio" name="MySel" value="Radio2"
onclick="__doPostBack('Radio2','')" language="javascript" /><label
for="Radio2">D81X</label></td><td></td><td><img
src=chain_images/D81X.gif></td><td>&nbsp;</td>

</tr><tr>

<td><input id="Radio3" type="radio" name="MySel" value="Radio3"
onclick="__doPostBack('Radio3','')" language="javascript" /><label
for="Radio3">D88K</label></td><td></td><td><img
src=chain_images/D88K.gif></td><td>&nbsp;</td>

</tr><tr>

<td><input id="Radio4" type="radio" name="MySel" value="Radio4"
onclick="__doPostBack('Radio4','')" language="javascript" /><label
for="Radio4">SC78</label></td><td></td><td><img
src=chain_images/SC78.gif></td><td>&nbsp;</td>

</tr>

</table>

I need to get this type of result:
<table id="Tbchain" border="0"
style="border-width:1px;border-style:solid;">

<tr>

<td><input id="Radio0" type="radio" name="MySel" value="Radio0"
checked="checked" onclick="__doPostBack('BindAssembly_Click','')"
language="javascript" /><label
for="Radio0">C188</label></td><td></td><td><img
src=chain_images/C188.gif></td><td>&nbsp;</td>

</tr><tr>

<td><input id="Radio1" type="radio" name="MySel" value="Radio1"
onclick="__doPostBack('BindAssembly_Click','')" language="javascript"
/><label for="Radio1">D667</label></td><td></td><td><img
src=chain_images/D667.gif></td><td>&nbsp;</td>

</tr><tr>

<td><input id="Radio2" type="radio" name="MySel" value="Radio2"
onclick="__doPostBack('BindAssembly_Click','')" language="javascript"
/><label for="Radio2">D81X</label></td><td></td><td><img
src=chain_images/D81X.gif></td><td>&nbsp;</td>

</tr><tr>

<td><input id="Radio3" type="radio" name="MySel" value="Radio3"
onclick="__doPostBack('BindAssembly_Click','')" language="javascript"
/><label for="Radio3">D88K</label></td><td></td><td><img
src=chain_images/D88K.gif></td><td>&nbsp;</td>

</tr><tr>

<td><input id="Radio4" type="radio" name="MySel" value="Radio4"
onclick="__doPostBack('BindAssembly_Click','')" language="javascript"
/><label for="Radio4">SC78</label></td><td></td><td><img
src=chain_images/SC78.gif></td><td>&nbsp;</td>

</tr>

</table>

Source Code
<%@ Page Language="VB" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>

<%@ import Namespace="System.Web.UI.WebControls" %>

<%@ import Namespace="System.Net" %>

<%@ import Namespace="System.Data.SqlClient" %>

<%@ import Namespace="System.Data" %>

<script language="VB" Debug="true" runat="server">

Public Const DB_CONN_STRING = "my connection"

Public cChainID as Integer

'Protected WithEvents optModify As
System.Web.UI.WebControls.RadioButton

Sub Page_Load(src As Object, e As EventArgs)

if not IsPostBack Then

BindManufacturer()

BindConveyorType()

BindChain()

BindAssembly()

End if

End Sub

Sub BindManufacturer()

Dim strConn=DB_CONN_STRING

Dim mycn As SqlConnection = new SqlConnection(strConn)

Dim SQL As String = "SELECT manufacturer FROM Sn_Rollers GROUP BY
manufacturer"

Dim myda As SqlDataAdapter = new SqlDataAdapter (SQL, mycn)

Dim ds As DataSet = new DataSet()

myda.Fill (ds,"Table1")

manufacturer.DataTextField="manufacturer"

manufacturer.DataValueField="manufacturer"

manufacturer.DataSource=ds

manufacturer.DataBind()

End Sub

Sub BindConveyorType()

Dim strConn=DB_CONN_STRING

Dim mycn As SqlConnection = new SqlConnection(strConn)

Dim manuf As String = manufacturer.selecteditem.value

Dim SQL As String = "SELECT manufacturer, convtp FROM Sn_Rollers
WHERE (manufacturer = '"+manuf+"') GROUP BY manufacturer, convtp"

Dim myda As SqlDataAdapter = new SqlDataAdapter (SQL, mycn)

Dim ds As DataSet = new DataSet()

myda.Fill (ds,"Table2")

convtype.DataTextField="convtp"

convtype.DataValueField="convtp"

convtype.DataSource=ds

convtype.DataBind()

BindChain()

End Sub

Sub BindChain()

Dim strConn=DB_CONN_STRING

Dim mycn As SqlConnection = new SqlConnection(strConn)

Dim Smanufacturer As String = manufacturer.selecteditem.value

Dim Sconvtype As String = convtype.selecteditem.value

Dim SQL As String = "SELECT manufacturer, convtp, chain FROM
Sn_Rollers WHERE (manufacturer = '"+Smanufacturer+"' AND convtp =
'"+Sconvtype+"') GROUP BY manufacturer, convtp, chain"

Response.Write("CHAIN <br>"+ SQL + "<br><br>" )

Dim myda As SqlDataAdapter = new SqlDataAdapter (SQL, mycn)

Dim ds As DataSet = new DataSet()

myda.Fill (ds,"Table3")

'chain.DataSource=ds

'chain.DataBind()

Dim i As integer

For i = 0 To ds.Tables("Table3").Rows.Count - 1

Dim tRow As New TableRow()

Dim tCell_1 As New TableCell()

Dim tCell_2 As New TableCell()

Dim tCell_3 As New TableCell()

Dim tCell_4 As New TableCell()

Rb = New RadioButton()

cChainID=i

Rb.ID = "Radio" + i.ToString()

Rb.Text = ds.Tables("Table3").Rows(i).Item(2).ToString()

Rb.GroupName="MySel"

************ Here is the problem
**************************************

AddHandler Rb.CheckedChanged, AddressOf BindAssembly_Click

' It is not linking the same sub event BindAssembly_Click() for each
radiobutton. Instead, it is adding an event with the ID name from
Rb.ID="Radio"+ i.ToString()

*************************** || **************************************

Rb.AutoPostBack = True

'Rb.Checked = True

tCell_1.Controls.Add(Rb)

tRow.Cells.Add(tCell_1)

tRow.Cells.Add(tCell_2)

Tbchain.Rows.Add(tRow)

tCell_3.Text="<img
src=chain_images/"+Replace(ds.Tables("Table3").Rows(i).Item(2).ToSt ring(),"
","") +".gif>"

tCell_4.Text="&nbsp;"

tRow.Cells.Add(tCell_3)

tRow.Cells.Add(tCell_4)

Tbchain.Rows.Add(tRow)

next

End Sub

function LoopTextBoxes(ByVal parent As Control) As String

Dim c As Control

Dim finish As Boolean = false

For Each c In parent.Controls

If c.GetType() Is GetType(RadioButton) Then

if Ctype(c, RadioButton).Checked=True then

LoopTextBoxes=Ctype(c, RadioButton).Text

exit function

end if

End If

If (c.HasControls) Then

LoopTextBoxes(c)

End If

Next

End function

Sub BindAssembly()

Dim strConn=DB_CONN_STRING

Dim mycn As SqlConnection = new SqlConnection(strConn)

Dim Smanufacturer As String = manufacturer.selecteditem.value

Dim Sconvtype As String = convtype.selecteditem.value

Dim Schain As string

Schain = LoopTextBoxes(Me)

Dim SQL As String = "SELECT manufacturer, convtp, chain, assembly
FROM Sn_Rollers WHERE (manufacturer = '"+Smanufacturer+"' AND convtp =
'"+Sconvtype+"' AND chain = '"+Schain +"') GROUP BY manufacturer,
convtp, chain, assembly"

Response.write(SQL)

Dim myda As SqlDataAdapter = new SqlDataAdapter (SQL, mycn)

Dim ds As DataSet = new DataSet()

myda.Fill (ds,"Table4")

Dpassembly.DataTextField="assembly"

Dpassembly.DataValueField="assembly"

Dpassembly.DataSource=ds

Dpassembly.DataBind()

End Sub

Sub BindConveyorType_Click(sender As Object, e As EventArgs)

BindConveyorType()

End Sub

Sub BindChain_Click(sender As Object, e As EventArgs)

BindChain()

End Sub

Sub BindAssembly_Click(sender As Object, e As EventArgs)

BindAssembly()

End Sub

</script>

Thanks

Rod
Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.