Solved

trying to bind to a Datalist key

Posted on 2009-07-09
17
280 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 350 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 150 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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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