Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8732243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:24:10+00:00 2026-06-13T09:24:10+00:00

I’m working on a context menu for items in my GridView, but there’s one

  • 0

I’m working on a context menu for items in my GridView, but there’s one major snag. I can’t seem to figure out how to send the PK value for the row that is right-clicked on to the JQuery that interacts with my HTTP Handler to update the database.

Basically, the context menu will have 3 items, Reply, Mark as Read, and Delete.
In each case I need the mailid value from the database to be available to the JQuery so that I can redirec the user to a page where they can reply to the selected mail or to tell the HTTP Handler which item in the database it’s supposed to update.

Here’s my code:

    protected void gvItems_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onClick"] = "getMailDetail(" + DataBinder.Eval(e.Row.DataItem, "mailid") + ")";
            e.Row.Attributes["id"] = DataBinder.Eval(e.Row.DataItem, "mailid");

            Label lblStatus = (Label)e.Row.FindControl("lblStatus");

            if (lblStatus.Text == "unread")
            {
                e.Row.Font.Bold = true;
            }
        }
    }

I’m setting the onClick attribute for the Rows so that clicking anywhere on the GridViewRow will trigger the page to display the mail in detail. I figured I could get the mailid value in the same way using a different attribute.

And on the .aspx…

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.contextMenu.js"></script>
<script type="text/javascript">
    function getMailDetail(mailId) {
        $.ajax({
            type: "GET",
            url: '<%= ResolveUrl("~/GetMail.ashx") %>',
            data: { mid: mailId },
            success: function (data) {
                var pnlList = $('#<%= pnlList.ClientID %>');
                var pnlMail = $('#<%= pnlMail.ClientID %>');
                var lblFrom = $('#<%= lblFrom.ClientID %>');
                var lblDate = $('#<%= lblDate.ClientID %>');
                var lblSubject = $('#<%= lblSubject.ClientID %>');
                var lblMessage = $('#<%= lblMessage.ClientID %>');

                lblFrom.text(data.From);
                lblDate.text(data.Date);
                lblSubject.text(data.Subject);
                lblMessage.text(data.Message);

                pnlList.css("display", "none");
                pnlMail.css("display", "block");
            }
        });
    }

    function markRead(mailId) {
        $.ajax({
            type: "POST",
            url: '<%= ResolveUrl("~/MailAction.ashx") %>',
            data: { mid: mailId, act: "markRead" },
            success: window.location = "Inbox.aspx"
        });
    }

    function deleteMail(mailId) {
        $.ajax({
            type: "POST",
            url: '<%= ResolveUrl("~/MailAction.ashx") %>',
            data: { mid: mailId, act: "Delete" },
            success: window.location = "Inbox.aspx"
        });
    }
</script>
<div class="columns">
    <div class="leftcol">
        <a href="Write.aspx" class="button">Compose Message</a>

        <ul>
            <li><a href="SentItems.aspx">Sent Items</a></li>
            <li class="active"><a href="Inbox.aspx">Inbox</a></li>
            <li><a href="DeletedItems.aspx">Deleted Items</a></li>
        </ul>
    </div>

    <div class="rightcol">
        <asp:Panel runat="server" ID="pnlList">
            <div class="rightalign">
                <asp:Label runat="server" ID="lblCount"></asp:Label>
            </div>

            <asp:GridView runat="server" ID="gvItems" DataKeyNames="mailid" 
                AutoGenerateColumns="false" onrowdatabound="gvItems_RowDataBound" 
                Width="100%">
                <Columns>
                    <asp:TemplateField ItemStyle-CssClass="centeralign">
                        <HeaderTemplate>
                            <asp:CheckBox runat="server" ID="chkSelectAll" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox runat="server" ID="chkSelect" />
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField ItemStyle-CssClass="hidden" HeaderStyle-CssClass="hidden">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblStatus" Text='<%# DataBinder.Eval(Container.DataItem, "status") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField ItemStyle-CssClass="hidden" HeaderStyle-CssClass="hidden" DataField="mailid" />
                    <asp:BoundField DataField="firstname" HeaderText="From" SortExpression="firstname" />
                    <asp:BoundField DataField="datesent" HeaderText="Date" SortExpression="datesent" DataFormatString="{0:yyyy/MM/dd HH:mm}" />
                    <asp:BoundField DataField="subject" HeaderText="Subject" SortExpression="subject" />
                </Columns>
            </asp:GridView>
        </asp:Panel>

        <asp:Panel runat="server" ID="pnlMail" cssclass="hidden">
            <p>From: <asp:Label runat="server" ID="lblFrom"></asp:Label><br />
                Sent On: <asp:Label runat="server" ID="lblDate"></asp:Label><br />
                Subject: <asp:Label runat="server" ID="lblSubject"></asp:Label></p>
            <p><asp:Label runat="server" ID="lblMessage"></asp:Label></p>
        </asp:Panel>
    </div>
</div>

<!-- Row Context Menu -->
<ul id="contextmenu" class="contextMenu">
    <li><a href="#reply">Reply</a></li>
    <li><a href="#mread">Mark As Read</a></li>
    <li><a href="#delete">Delete</a></li>
</ul>

<script type="text/javascript">
    $(document.ready(function() {
        $(".dataRow").contextMenu({
            menu: 'contextmenu' },
            function(action, el, pos, mid) {
                contextMenuWork(action, el, pos);
            }
        );
    },
    function contextMenuWork(action, el, pos) {
        var mid = $(el).attr("id");
        switch (action) {
            case "reply":
            {
                window.location = "Reply.aspx?id=" + mid;
                break;
            }
            case "mread":
            {
                markRead(mid);
                break;
            }
            case "delete":
            {
                deleteMail(mid);
                break;
            }
        }
    }
);
</script>

Unfortunately there seems to be a problem in my approach here as I’m getting an InvalidCastException on the assignment of the id attribute on the newly bound GridViewRow:

Cannot implicitely convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)

So, I replace that line with this

e.Row.Attributes["id"] = (string)DataBinder.Eval(e.Row.DataItem, "mailid");

And now, although there are no errors that Visual Studio can tell me about at design time, at runtime, I get the following InvalidCastException:

Unable to cast object of type ‘System.Int32’ to type ‘System.String’.

I have no idea where the integer is coming from here so I don’t know how to proceed.

Any help will be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:24:11+00:00Added an answer on June 13, 2026 at 9:24 am

    I never seemed able to get a working cast on the value here, so what I did was to change the RowDataBound void to the following:

        protected void gvItems_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var mailid = DataBinder.Eval(e.Row.DataItem, "mailid");
                e.Row.Attributes["onClick"] = "getMailDetail(" + DataBinder.Eval(e.Row.DataItem, "mailid") + ")";
                e.Row.Attributes["id"] = mailid.ToString();
    
                Label lblStatus = (Label)e.Row.FindControl("lblStatus");
    
                if (lblStatus.Text == "unread")
                {
                    e.Row.Font.Bold = true;
                }
            }
        }
    

    Declaring a new var and with the DataItem value for “mailid” and then giving it a .ToString() fixed all my problems

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.