Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

value of LinkButton in DataList? 1

Status
Not open for further replies.

Sheffield

Programmer
Jun 1, 2001
180
0
0
US
Greetings,

Problem: I'm unable to determine the Value or Text of a 'clicked' LinkButton located within a DataList.

Is this even possible? How do I even assign the LinkButton a value?

My goal is to: (A.) click on a LinkButton; (B.)display a DropDownList in its place (which works); (C.)Select an option from the DropDownList and (D.) eventually Update a table using the values of both the DropDownList AND LinkButton.

My SQL that populates the DataList is: "SELECT J_ID, J_Name FROM tblTest"


However, in order to UPDATE the table via the OnSelectedIndexChanged event of the DropDownList, I first need to know the Text (J_Name) or Value (J_ID) of the clicked LinkButton.

HELP!!!!


Code:
<asp:DataList ID="dlst_Test" OnItemCommand= "dlstTest_ItemCommand" Runat="Server">

<HeaderTemplate>    
  <table>
    <tr>
       <td>Requirement</td>
    </tr>
</HeaderTemplate>

<ItemTemplate>
    <tr>
        <td><asp:LinkButton Text='<%# Container.DataItem( "J_Name" ) %>' Runat="Server" /></td>
    </tr>
</ItemTemplate>

<SelectedItemTemplate>
     <tr>
         <td><asp:DropDownList id="drpJ_Name" OnSelectedIndexChanged="drpJ_Name_SelectedIndexChanged" AutoPostBack="True" Runat="Server">
             </asp:DropDownList></td>
     </tr>
</SelectedItemTemplate>

<FooterTemplate>
    </table>
</FooterTemplate>
</asp:DataList>

The LinkButton appears to cease being an object once its clicked.

I feel so clueless.....

I greatly appreciate any help you can provide.

Thanks:)
 
The problem is that the LinkButton is in your ItemTemplate, and the DropDownList is in your SelectedItemTemplate.

So once you 'select' an item, and that template is loaded, then yes, the LinkButton is gone, because it's been replaced by the SelectedItemTemplate, which contains the DropDownList.

You can, however, pass something to your EventHandler via a CommandArgument, and then persist that value in ViewState until such time that you need it.

For example:

Code:
<ItemTemplate>
    <tr>
        <td><asp:LinkButton Text='<%# Container.DataItem( "J_Name" ) %>' Runat="Server" [b]CommandArgument="J_Name"[/b] /></td>
    </tr>
</ItemTemplate>

This value will then be accessible via the CommandEventArgs that are passed as an argument to your EventHandler, dlstTest_ItemCommand:

Code:
ViewState["clickedItem"] = e.CommandArgument.ToString();

which you can access later in your SelectedIndexChanged EventHandler, drpJ_Name_SelectedIndexChanged:

Code:
if(ViewState["clickedItem"].ToString() == "J_Name")
  //handle the jname case
else
  //handle the jid case

:)
paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top