Monday, March 26, 2012

working with datalists

if you have a datalist, can disable and enable rows in that datalist with radiobuttons?

Can you explain what do you mean by enable/disable? What kind of info does the datalist contain and why/what kind of info do you want to disable/enable?

im designing an online voting system, so i will have many candidates running for a position.

Lets say i have 5 candidates, that would be 5 rows and each row would have a radiobutton. if you clicked that radiobutton it would disabel that candidate. you could select all candidates until they were all disabled. i would then have a way to enable each candidate if i wanted too.

does that make sense?


Here is a working (but not pretty) version.

<scriptrunat="server">

Dim sourceAsNew Data.DataTable

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)

IfNot IsPostBackThen

source.Columns.Add("PID",GetType(Integer))

source.Columns.Add("Position",GetType(String))

source.Columns.Add("CID",GetType(Integer))

source.Columns.Add("Candidate",GetType(String))

source.Columns.Add("IsActive",GetType(Boolean))

Dim rowAs Data.DataRow

For iAsInteger = 1To 2

For jAsInteger = 1To 4

row = source.NewRow()

row(0) = i

row(1) ="Position " & i.ToString()

row(2) = j

row(3) ="Candidate " & j.ToString()

row(4) = 0

source.Rows.Add(row)

Next

Next

GV.DataSource = source

GV.DataBind()

EndIf

EndSub

ProtectedSub IsCActive_CheckedChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)

Dim cboxAs CheckBox =CType(sender, CheckBox)

If cbox.Checked =TrueThen

' Disable that row

' I am only disabling the CheckBox as all other

' fields are already in readonly mode

cbox.Enabled =False

EndIf

EndSub

</script>

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:GridViewID="GV"runat="server"AutoGenerateColumns="false">

<Columns>

<asp:BoundFieldDataField="PID"ReadOnly="true"/>

<asp:BoundFieldDataField="Position"ReadOnly="true"/>

<asp:BoundFieldDataField="Candidate"ReadOnly="true"/>

<asp:TemplateField>

<ItemTemplate>

<asp:CheckBoxID="IsCActive"runat="server"Checked='<%# Cbool(Eval("IsActive")) %>'OnCheckedChanged="IsCActive_CheckedChanged"AutoPostBack="true"/>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

<asp:LiteralID="Message"runat="server"EnableViewState="false"/>

</div>

</form>

</body>

</html>

0 comments:

Post a Comment