DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development > ASP.Net Development

Discuss "Finding Datalist Row on auto postback" in the ASP.Net Development forum.

ASP.Net Development - Learn best coding practices and tips to get the best out of your ASP.Net applications.


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-11-2008, 06:32 AM
richyrich's Avatar
Moderator

 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 173
Thanks: 12
Thanked 3 Times in 3 Posts
Rep Power: 1
richyrich is on a distinguished road
Default Finding Datalist Row on auto postback

This is a bit of a complicated one and I can't quite figure out how to approach it.

I have a datalist that shows data in dropdowns and text boxes. They are all contained within table cells that runat the server to enable me to toggle the visibility easily.

The first cell has a dropdown that controls what other dropdowns and text boxes should be shown in that row. What I want to do is when this dropdown is changed do an autopostback which will toggle the visibility of the other table cells within that row.

My problem is when the dropdown is changed and posts back, how do I know which datalist item the dropdown come from? In addition, how do I then find the controls within the datalist that I need to change?

The other related issue is I have a button that allows users to add another item to the datalist, based on the same format. This is contained within the footer of the datalist, so it always appears at the bottom of the current list. When they click the button another row appears (the footer) just showing the first column dropdown (this bit works fine). When they make a selection, I want the footer to show the correct dropdowns and textboxes based on the selection made.

Again, how can I tell that it is the footer dropdown that has been changed and then how can I find the relevant controls to allow me to toggle the visibility of the various cells I need to hide/show?

Here's the datalist code:-
Code:
<asp:datalist ID="dlst_premiums" runat="server" OnUpdateCommand="butt_show_add_premium" OnItemDataBound="dlst_premiums_onitemdatabound">

<ItemTemplate>
<tr>
<td id="td_source_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8">Source</td>
<td id="td_transfer_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8">Transfer from</td>
<td id="td_premium_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8">Premium</td>
<td id="td_frequency_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8">Frequency</td>
<td id="td_premiumtype_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8">Type</td>
<td id="td_level_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8">Level/Incr</td>
<td id="td_incrby_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8">By</td>
<td id="td_status_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8">Status</td>
</tr>
<tr>
<td id="td_source" runat="server" style="padding:1px 2px 1px 2px;" class="dblue8">
<asp:DropDownList ID="ddl_source" runat="server" CssClass="registerform7" OnSelectedIndexChanged="change_premium_source" />
</td>
<td id="td_transfer" runat="server" style="padding:1px 2px 1px 2px;" class="dblue8">
<asp:DropDownList ID="ddl_transfer" runat="server" CssClass="registerform7" />
</td>
<td id="td_premium" runat="server" align="center" style="padding:1px 2px 1px 2px;" class="dblue8">
£<asp:textbox ID="txt_premium" runat="server" MaxLength="20" Width="80px" CssClass="registerform7" />
</td>
<td id="td_frequency" runat="server" style="padding:1px 2px 1px 2px;" class="dblue8">
<asp:DropDownList ID="ddl_frequency" runat="server" CssClass="registerform7">
<asp:ListItem Text="Please Select" Value="" />
<asp:ListItem Text="Monthly" />
<asp:ListItem Text="Annually" />
<asp:ListItem Text="Quarterly" />
</asp:DropDownList>
</td>
<td id="td_premiumtype" runat="server" style="padding:1px 2px 1px 2px;" class="dblue8">
<asp:DropDownList ID="ddl_premiumtype" runat="server" CssClass="registerform7" />
</td>
<td id="td_level" runat="server" style="padding:1px 2px 1px 2px;" class="dblue8">
<asp:DropDownList ID="ddl_level" runat="server" CssClass="registerform7">
<asp:ListItem Text="Level" Value="lev" />
<asp:ListItem Text="Increasing" Value="inc" />
</asp:DropDownList>
</td>
<td id="td_incrby" runat="server" style="padding:1px 2px 1px 2px;" class="dblue8">
<asp:textbox ID="txt_incrby" runat="server" MaxLength="3" Width="20px" CssClass="registerform7" />
</td>
<td id="td_status" runat="server" style="padding:1px 2px 1px 2px;" class="dblue8">
<asp:DropDownList ID="ddl_status" runat="server" CssClass="registerform7" />
</td>
</tr>
</ItemTemplate>
 
<FooterTemplate>
<tr id="tr_addpremium_h" runat="server">
<td id="td_add" runat="server" style="padding:8px 0px 2px 5px;" class="dblue8"><asp:ImageButton ID="butt_showadd" runat="server" ImageUrl="link_add1.gif" ToolTip="Click to add a premium" CommandName="Update" /></td>
<td id="td_source_h_add" runat="server" style="text-align:center;padding:1px 2px 1px 2px;width:10%;" class="dblue8" visible="false">Source</td>
<td id="td_transfer_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8" visible="false">Transfer from</td>
<td id="td_premium_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8" visible="false">Premium</td>
<td id="td_frequency_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8" visible="false">Frequency</td>
<td id="td_premiumtype_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8" visible="false">Type</td>
<td id="td_level_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8" visible="false">Level/Incr</td>
<td id="td_incrby_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8" visible="false">By</td>
<td id="td_status_h" runat="server" style="text-align:center;padding:1px 2px 1px 2px;" class="dblue8" visible="false">Status</td>
</tr>
<tr id="tr_addpremium" runat="server" visible="false">
<td id="td_source_add" runat="server" style="padding:1px 2px 1px 2px;width:10%;" class="dblue8" visible="false">
<asp:DropDownList ID="ddl_source_add" runat="server" CssClass="registerform7" AutoPostBack="true" OnSelectedIndexChanged="change_premium_source" />
</td>
<td id="td_transfer" runat="server" style="padding:1px 2px 1px 2px;" class="dblue8" visible="false">
<asp:DropDownList ID="ddl_transfer_add" runat="server" CssClass="registerform7" />
</td>
<td id="td_premium" runat="server" align="center" style="padding:1px 2px 1px 2px;" class="dblue8" visible="false">
£<asp:textbox ID="txt_premium_add" runat="server" MaxLength="20" Width="80px" CssClass="registerform7" />
</td>
<td id="td_frequency" runat="server" style="padding:1px 2px 1px 2px;" class="dblue8" visible="false">
<asp:DropDownList ID="ddl_frequency_add" runat="server" CssClass="registerform7">
<asp:ListItem Text="Please Select" Value="" />
<asp:ListItem Text="Monthly" />
<asp:ListItem Text="Annually" />
<asp:ListItem Text="Quarterly" />
</asp:DropDownList>
</td>
<td id="td_premiumtype" runat="server" style="padding:1px 2px 1px 2px;" class="dblue8" visible="false">
<asp:DropDownList ID="ddl_premiumtype_add" runat="server" CssClass="registerform7" />
</td>
<td id="td_level" runat="server" style="padding:1px 2px 1px 2px;" class="dblue8" visible="false">
<asp:DropDownList ID="ddl_level_add" runat="server" CssClass="registerform7">
<asp:ListItem Text="Level" Value="lev" />
<asp:ListItem Text="Increasing" Value="inc" />
</asp:DropDownList>
</td>
<td id="td_incrby" runat="server" style="padding:1px 2px 1px 2px;" class="dblue8" visible="false">
<asp:textbox ID="txt_incrby_add" runat="server" MaxLength="3" Width="20px" CssClass="registerform7" />
</td>
<td id="td_status" runat="server" style="padding:1px 2px 1px 2px;" class="dblue8" visible="false">
<asp:DropDownList ID="ddl_status_add" runat="server" CssClass="registerform7" />
</td>
</tr>
</FooterTemplate>
</asp:datalist>
The 2 dropdowns I've highlighted are the first column ones. When these are changed, I need to know which row (or footer) of the datalist they're from and then know how to access the controls within that row.

Hope that all makes sense....
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-12-2008, 04:52 AM
richyrich's Avatar
Moderator

 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 173
Thanks: 12
Thanked 3 Times in 3 Posts
Rep Power: 1
richyrich is on a distinguished road
Default

Think I've worked out a solution.

In the OnSelectedIndexChanged event, declare the dropdownlist as an object and then use the .parent method to go back up to the datalist item control.

Code:
Sub change_ddl_level(ByVal s As Object, ByVal e As EventArgs)
        Dim ddl_id As DropDownList = s
        Dim dlst_item As DataListItem = s.parent.parent 'this finds the datalist item
        Dim td_incrby_h As HtmlTableCell = dlst_item.FindControl("td_incrby_h")
        Dim td_incrby As HtmlTableCell = dlst_item.FindControl("td_incrby")
        If ddl_id.SelectedValue = "inc" Then
            td_incrby_h.Visible = True
            td_incrby.Visible = True
        Else
            td_incrby_h.Visible = False
            td_incrby.Visible = False
        End If
End Sub
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > .Net Development > ASP.Net Development

Bookmarks

Tags
autopostback, datalist, dropdownlist, textbox

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using AJAX Auto Complete Extender with Database richyrich ASP.Net 0 03-26-2008 07:59 AM


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.


All times are GMT -4. The time now is 12:58 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0 RC7
©2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45