Solved

trying to bind to a Datalist key

Posted on 2009-07-09
17
266 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
  • 8
  • 5
  • 3
  • +1
17 Comments
 
LVL 41

Expert Comment

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

Expert Comment

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

Expert Comment

by:wrwilson
Comment Utility
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
 

Author Comment

by:ratkinso666
Comment Utility
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:wrwilson
Comment Utility
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
Comment Utility
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
Comment Utility
yes, wrwilson, it is -1, I am "selecting" with the button I just mentioned above
0
 
LVL 7

Expert Comment

by:wrwilson
Comment Utility
Where is the button?  Can you post more/most of your code so we can see how it all ties together?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

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

Author Comment

by:ratkinso666
Comment Utility

<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 350 total points
Comment Utility
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:wrwilson
wrwilson earned 150 total points
Comment Utility
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:wrwilson
Comment Utility
BTW, I'm a Randy too...
0
 

Author Comment

by:ratkinso666
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now