Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

trying to bind to a Datalist key

Posted on 2009-07-09
17
Medium Priority
?
290 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:ratkinso666
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 3
  • +1
17 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 24820110
For a particular item you should be doing that work in ItemCommand event
0
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 24820137
You might try: Dim MerchandiseId as String= DataList2.DataKeys(DataList2.SelectedIndex)
0
 
LVL 7

Expert Comment

by:Randy Wilson
ID: 24823091
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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:ratkinso666
ID: 24823568
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
 
LVL 7

Expert Comment

by:Randy Wilson
ID: 24823624
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
 

Author Comment

by:ratkinso666
ID: 24823638
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
 

Author Comment

by:ratkinso666
ID: 24823650
yes, wrwilson, it is -1, I am "selecting" with the button I just mentioned above
0
 
LVL 7

Expert Comment

by:Randy Wilson
ID: 24823721
Where is the button?  Can you post more/most of your code so we can see how it all ties together?
0
 

Author Comment

by:ratkinso666
ID: 24823767
will, do, but real quick I can tell you that the button is in the Datalist
0
 

Author Comment

by:ratkinso666
ID: 24823824

<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
 
LVL 41

Accepted Solution

by:
guru_sami earned 1400 total points
ID: 24823965
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
 
LVL 7

Assisted Solution

by:Randy Wilson
Randy Wilson earned 600 total points
ID: 24824096
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
 
LVL 7

Expert Comment

by:Randy Wilson
ID: 24824110
BTW, I'm a Randy too...
0
 

Author Comment

by:ratkinso666
ID: 24824883
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
 
LVL 41

Expert Comment

by:guru_sami
ID: 24824938
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
 

Author Comment

by:ratkinso666
ID: 24825083
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
 

Author Closing Comment

by:ratkinso666
ID: 31601911
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

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question