Link to home
Start Free TrialLog in
Avatar of ratkinso666
ratkinso666

asked on

trying to bind to a Datalist key

Hi all, I have a datalist as such:
<asp:DataList ID="DataList2" runat="server" BackColor="White"
        BorderColor="#FFCCFF" BorderStyle="Solid" BorderWidth="2px" CellPadding="4"
         datasourceid="SqlDataSource2" ForeColor="Black"
        GridLines="Vertical" RepeatColumns="4" RepeatDirection="Horizontal"
        DataKeyField="MerchandiseId">

When I go to the code-behind, I am trying to use that DataKeyField, but can't seem to find it correctly.  If I do the following I will get the first record and can go on through them one at a time, so I know it is there:

Dim MerchandiseId As String = DataList2.DataKeys(0).ToString()

But of course I only want the one I am clicking on, I would think the following would work, but I get an error, (item is not a member of system.eventArgs) can someone please tell me what small syntax I have wrong...

Dim MerchandiseId as String= DataList2.DataKeys(e.Item.ItemIndex)

I have tried using SelectedIndex and several other things, but don't seem to be able to find the correct combination.

Thanks,
Randy
Avatar of guru_sami
guru_sami
Flag of United States of America image

For a particular item you should be doing that work in ItemCommand event
Avatar of funwithdotnet
funwithdotnet

You might try: Dim MerchandiseId as String= DataList2.DataKeys(DataList2.SelectedIndex)
There a number of things you must do to "select" items in a DataList, see this

http://msdn.microsoft.com/en-us/library/75670ez0.aspx
Avatar of ratkinso666

ASKER

funwithdotnet, I tried what you said, but I have tried that before and this is the error I get:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Thanks
I bet it is "griping" about DataList2.SelectedIndex, check it in Debug and I bet it is -1 all the time.  How are you "selecting" an item in the DataList?
Perhaps the problem is that I am doing this in the button click event?  Instead of the SelectedIndexChange?? I am pressing a button and need to know the MerchandiseId of the item I Selected.

Protected Sub BtnPlaceBid_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ID As String = User.Identity.Name
        Dim CurrentDate As DateTime = Now()
        Dim MerchandiseId As String = 4
        Dim MerchandiseId2 As String = DataList2.DataKeys(DataList2.SelectedIndex) - gives me the error I just mentioned above
yes, wrwilson, it is -1, I am "selecting" with the button I just mentioned above
Where is the button?  Can you post more/most of your code so we can see how it all ties together?
will, do, but real quick I can tell you that the button is in the Datalist

<asp:DataList ID="DataList2" runat="server" BackColor="White" 
        BorderColor="#FFCCFF" BorderStyle="Solid" BorderWidth="2px" CellPadding="4" 
         datasourceid="SqlDataSource2" ForeColor="Black" 
        GridLines="Vertical" RepeatColumns="4" RepeatDirection="Horizontal" 
        DataKeyField="MerchandiseId">
        <FooterStyle BackColor="#CCCC99" BorderColor="#FFCCFF" BorderStyle="Solid" 
            BorderWidth="2px" />
        <EditItemStyle BorderColor="#FFCCFF" BorderStyle="Solid" BorderWidth="2px" />
        <ItemTemplate>
            <asp:TextBox ID="TextBox4" runat="server" BorderStyle="None" Height="25px" 
                Style="background-color: transparent" Text='<%# Eval("Description") %>' 
                TextMode="SingleLine" Width="192px"></asp:TextBox>
            <br />
            <asp:TextBox ID="TextBox2" runat="server" BorderStyle="None" Height="25px" 
                Style="background-color: transparent" Text='<%# Eval("SRP") %>' Width="198px"></asp:TextBox>
                <asp:TextBox ID="TextBox5" runat="server" BorderStyle="None" Height="25px" 
                Style="background-color: transparent" Text='<%# Eval("MerchandiseId") %>' Width="198px"></asp:TextBox>
            <br />
            <asp:HyperLink ID="HyperLink1" runat="server" Height="80px" 
                ImageUrl='<%# Eval("Graphic1") %>' NavigateUrl='<%# Eval("DetailPage") %>' 
                Target="_new" Text='<%# Eval("DetailPage") %>' Width="200px"></asp:HyperLink>
            <br />
            <br />
            <br />
            <br />
            <br />
            <br />
            <br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button ID="BtnPlaceBid" runat="server" Font-Bold="True" Font-Size="Medium" 
                ForeColor="Blue" onclick="BtnPlaceBid_Click" Text="Place Bid" />
            <br />
            <br />
            <br />
        </ItemTemplate>
        <AlternatingItemStyle BackColor="White" BorderColor="#FFCCFF" 
            BorderStyle="Solid" BorderWidth="2px" />
        <ItemStyle BackColor="#F7F7DE" BorderColor="#FFCCFF" BorderStyle="Solid" 
            BorderWidth="2px" />
        <SeparatorStyle BorderColor="#FFCCFF" BorderStyle="Solid" BorderWidth="2px" />
        <SelectedItemStyle BackColor="#CE5D5A" BorderColor="#FFCCFF" 
            BorderStyle="Solid" BorderWidth="2px" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#6B696B" BorderColor="#FFCCFF" BorderStyle="Solid" 
            BorderWidth="2px" Font-Bold="True" ForeColor="White" />
    </asp:DataList>
 
Code-behind:
Protected Sub BtnPlaceBid_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ID As String = User.Identity.Name
        Dim CurrentDate As DateTime = Now()
        Dim MerchandiseId As String = 4
        'Dim MerchandiseId2 As String = DataList2.DataKeys(DataList2.SelectedIndex)
 
       
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BTW, I'm a Randy too...
That is SO very close, if I take out the if command I can get what I want, it seems that the button does not give the "selected" value, e.CommandName is empty.  Do I really need to check for selected?
if you just have one button on it is not necessary...you can remove..
but may be you did not set CommandName attribute like this:
 <asp:Button ID="btnSelect" CommandName="Select" runat="server" Text="Button" />
That was it guru_sami, thank you very much for your and wrwilsons help!!  I will split the points between you, hope that is fair...

Thanks,
Randy
Thank you BOTH for your help, since guru_sami gave me the actual code I needed, I gave him more points, but thank you BOTH!!!