Adding selectable ASP options to Custom Web Controls

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Hello,

One thing I am looking to do is to create a couple of different custom
Web Controls that can give me added options when specifying them
in an ASP.NET page. For example:

<asp:RadioButtonList id=RadioButtonList1 runat="server">
<asp:ListItem myvalue1="x" myvalue2="y">Item 1</asp:ListItem>
<asp:ListItem myvalue1="a" myvalue2="b">Item 2</asp:ListItem>
<asp:ListItem myvalue1="g" myvalue2="h">Item 3</asp:ListItem>
</asp:RadioButtonList>

What I did was to create a custom Web Control and created get/set statements
for myvalue1 and myvalue2. I actually placed the value of the DataTextField
into a HashTable so the value I was setting could be stored and retrieved
depending on the listItem and/or what was selected. I am having problems
getting
this to work. Am I concentrating on the right area to get this all to work?

Thanks,
Peter
 
Hi Peter,

I think you're going to run into trouble because you're trying to make HTML
do something that it wasn't intended to do. Rather than mess with illegal
attributes, how about just separating the values with a pipe (|) or other
character and then parsing the selected value.

Here's some samble code in VB (VS 2005). Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>
<script runat="server">
Protected Sub RadioButtonList1_SelectedIndexChanged _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myvalue As String()
myvalue = Split(RadioButtonList1.SelectedItem.Value, "|")
Label1.Text = "myvalue1=" & myvalue(0) & " : myvalue2=" & myvalue(1)
End Sub
</script>

<html>
<head runat="server">
<title>Multi List</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButtonList id="RadioButtonList1" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Value="x|y">Item 1</asp:ListItem>
<asp:ListItem Value="a|b">Item 2</asp:ListItem>
<asp:ListItem Value="g|h">Item 3</asp:ListItem>
</asp:RadioButtonList><br />
&nbsp;<asp:Label ID="Label1" runat="server"></asp:Label></div>
</form>
</body>
</html>
 
Thanks for very much for the reply Ken. Yes, I was originally thinking in
the
direction you mentioned but there are multiple components where I need to
store these dual values and one of them is a textbox. However now that
I know this is not doable I will go with the strategy of populating the
DataValueField.
As for the textboxes I can use the tooltip field. I can write a function to
sort
it all out for me.

BTW, the reason I wanted the more elegant solution is that I was hoping once
I got this all together was to have non-technical type people to build these
scripts
as needed because they seem to make almost daily changes. However if I spell
it
out for them it hopefully won't be too bad...

Thanks,
Peter


Ken Cox said:
Hi Peter,

I think you're going to run into trouble because you're trying to make
HTML do something that it wasn't intended to do. Rather than mess with
illegal attributes, how about just separating the values with a pipe (|)
or other character and then parsing the selected value.

Here's some samble code in VB (VS 2005). Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>
<script runat="server">
Protected Sub RadioButtonList1_SelectedIndexChanged _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myvalue As String()
myvalue = Split(RadioButtonList1.SelectedItem.Value, "|")
Label1.Text = "myvalue1=" & myvalue(0) & " : myvalue2=" &
myvalue(1)
End Sub
</script>

<html>
<head runat="server">
<title>Multi List</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButtonList id="RadioButtonList1" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Value="x|y">Item 1</asp:ListItem>
<asp:ListItem Value="a|b">Item 2</asp:ListItem>
<asp:ListItem Value="g|h">Item 3</asp:ListItem>
</asp:RadioButtonList><br />
&nbsp;<asp:Label ID="Label1" runat="server"></asp:Label></div>
</form>
</body>
</html>

Peter said:
Hello,

One thing I am looking to do is to create a couple of different custom
Web Controls that can give me added options when specifying them
in an ASP.NET page. For example:

<asp:RadioButtonList id=RadioButtonList1 runat="server">
<asp:ListItem myvalue1="x" myvalue2="y">Item 1</asp:ListItem>
<asp:ListItem myvalue1="a" myvalue2="b">Item 2</asp:ListItem>
<asp:ListItem myvalue1="g" myvalue2="h">Item 3</asp:ListItem>
</asp:RadioButtonList>

What I did was to create a custom Web Control and created get/set
statements
for myvalue1 and myvalue2. I actually placed the value of the
DataTextField
into a HashTable so the value I was setting could be stored and retrieved
depending on the listItem and/or what was selected. I am having problems
getting
this to work. Am I concentrating on the right area to get this all to
work?

Thanks,
Peter
 
Hi Peter,

Another approach would be to use a set of parallel hidden fields to hold the
extra values. You would have to match them up to the visible field in some
way, perhaps through a naming convention.

Ken
 
Back
Top