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
ratkinso666Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guru_samiCommented:
For a particular item you should be doing that work in ItemCommand event
0
funwithdotnetCommented:
You might try: Dim MerchandiseId as String= DataList2.DataKeys(DataList2.SelectedIndex)
0
Randy Wilson.Net DeveloperCommented:
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
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

ratkinso666Author Commented:
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
0
Randy Wilson.Net DeveloperCommented:
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?
0
ratkinso666Author Commented:
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
0
ratkinso666Author Commented:
yes, wrwilson, it is -1, I am "selecting" with the button I just mentioned above
0
Randy Wilson.Net DeveloperCommented:
Where is the button?  Can you post more/most of your code so we can see how it all ties together?
0
ratkinso666Author Commented:
will, do, but real quick I can tell you that the button is in the Datalist
0
ratkinso666Author Commented:

<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

0
guru_samiCommented:
Do not use Click Event Instead:
1: As said Earlier you should be using ItemCommand of DataList like sample below:
<asp:DataList ID="DataList2" runat="server" DataKeyField="UserId"
            DataSourceID="SqlDataSource3" onitemcommand="DataList2_ItemCommand" >
        <ItemTemplate>
            <asp:Label ID="lblUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
            <asp:Button ID="btnSelect" CommandName="Select" runat="server" Text="Button" />
        </ItemTemplate>
        </asp:DataList>
protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            string key = DataList2.DataKeys[e.Item.ItemIndex].ToString();
            Response.Write(key);
        }
    }

Note: You button has CommanName property set to Select...it could be any name but then your ItemCommand code needs to be adjusted accordingly.

2:Now if you don't want to use ItemCommand event then you should be using Command Event of the button then you can pass your key as CommandArgument.
Let me know if you want to use this second method.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Randy Wilson.Net DeveloperCommented:
Thanks guru, this is what I was getting at ratkinso666, you must set up a selection somehow, the DataList has not natural SelectedItem like aa grid or detailsview.  guru_sami's  solution is one of many (as good as any other) that will work.
0
Randy Wilson.Net DeveloperCommented:
BTW, I'm a Randy too...
0
ratkinso666Author Commented:
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?
0
guru_samiCommented:
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" />
0
ratkinso666Author Commented:
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
0
ratkinso666Author Commented:
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!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.