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

  • Home
  • SEARCH
  • 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 6121443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:47:04+00:00 2026-05-23T15:47:04+00:00

So I have scoured the internet for a solution to this problem, and haven’t

  • 0

So I have scoured the internet for a solution to this problem, and haven’t been to successful so far. I am trying to update a SQL database based on “highlighted” rows in a GridView in ASP.NET. Here is the code I have so far for highlighting.

// ASP.NET

// GridView1 Row DataBound event: adds selection functionality
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Attributes.Add("onmousedown", "IsMouseDown(this)");
    e.Row.Attributes.Add("onmouseup", "IsMouseDown(this)");
    e.Row.Attributes.Add("onmouseover", "HighlightRow(this)");
}

// Javascript

<script type="text/javascript">
    var mousedown = false;

    document.onselectstart = new Function ("return false")

    function IsMouseDown(row) {
        if (mousedown == false) mousedown = true;
        else mousedown = false

        if (mousedown == true) {
            HighlightRow(row)
        }
    }

    function HighlightRow(row) {
        if (mousedown == true) {
            if (row.className == 'gridHighlightedRow') {
                row.className = 'gridNormalRow';
            }
            else {
                row.className = 'gridHighlightedRow';
            }
        }
    }
</script>

// CSS Classes

.gridNormalRow
{
    background-color: #FFFFFF; 
}
.gridHighlightedRow
{
    background-color: #FFFFCC;
}

The above code is working perfectly, the problem is that I can’t do anything with the highlighted rows because Javascript (based on my understanding) modiefies the tr tag, not the actual GridView class or BackColor. I have looked all over the place for a solution, all I need to do is find some identifier I can access from my C# code to update each row. Anyone have any ideas?

Quick Edit

I actually thought of using some type of hidden field, I just have no idea how to do that with JavaScript or what I would need to do the the Row.Attributes.Add() to make it save to a hidden field. Can I see some examples of what I would need to do (JavaScript is not my strong suite)

  • 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-05-23T15:47:05+00:00Added an answer on May 23, 2026 at 3:47 pm

    So I figured it out. It is a little messy but it works till I refine what I have.

    First I sent the Row Index to javascript

    // C#

    // GridView1 Row DataBound event: adds selection functionality
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // Excludes row header and pager rows
        if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Pager)
        {
            e.Row.Attributes.Add("onmousedown", "IsMouseDown(this," + e.Row.RowIndex + ")");
            e.Row.Attributes.Add("onmouseup", "IsMouseDown(this," + e.Row.RowIndex + ")");
            e.Row.Attributes.Add("onmouseover", "HighlightRow(this," + e.Row.RowIndex + ")");
        }
    }
    

    Then did the highlighting and for each row moused over with when mousedown was true I appended a string. When the mouseup event fires I did a postback of the string

    // Javascript

    <script type="text/javascript">
        var mousedown = false;
        var returnGrid = "";
    
        document.onselectstart = new Function ("return false")
    
        function IsMouseDown(row, index) {
            if (mousedown == false) mousedown = true;
            else mousedown = false
    
            if (mousedown == true) {
                HighlightRow(row, index)
            }
            else
            {
                __doPostBack("ReturnedIndexes", returnGrid);
            }
        }
    
        function HighlightRow(row, index) {
            if (mousedown == true) {
                if (row.className == 'gridHighlightedRow') {
                    row.className = 'gridNormalRow';
                    returnGrid += (index + ",");
                }
                else {
                    row.className = 'gridHighlightedRow';
                    returnGrid += (index + ",");
                }
            }
        }
    </script>
    

    Finally, when the page loads with the “ReturnIndexes” event target I set the row color based on what the row color currently is.

    // C#

    // Page load event
    protected void Page_Load(object sender, EventArgs e)
    {
        // Avoids calling this code if the call is a postback
        if (!IsPostBack)
        {
            // Some Code Here
        }
        else if(Request.Params.Get("__EVENTTARGET").ToString() == "ReturnedIndexes")
        {
            // Returns highlighted results
            String ReturnIndexes = Request.Params.Get("__EVENTARGUMENT").ToString();
    
            txtRowIndexes.Text = ReturnIndexes;
    
            int[] GridIndex = RowHighlightChanged();
    
            for (int i = 0; i < GridIndex.Length; i++)
            {
                if (GridView1.Rows[GridIndex[i]].CssClass == "gridHighlightedRow")
                {
                    GridView1.Rows[GridIndex[i]].CssClass = "gridNormalRow";
                }
                else GridView1.Rows[GridIndex[i]].CssClass = "gridHighlightedRow";
            }
        }
    }
    

    Finally to update the “highlighted” rows I just look for the CssClass “gridHighlightedRow”.

    // C#

    // buttonUpdateSelected Click event: Updates all items currently selected in the grid view
    protected void buttonUpdateSelected_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.CssClass == "gridHighlightedRow")
            {
                // Update Rows
            }
        }
    }
    

    Works pretty well I think :).

    Thanks for the help Thomas, it got me started on the right track!

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

Sidebar

Related Questions

I have scoured the internet looking for a solution to this and I am
I have scoured the web in search of a solution to this problem, but
I've scoured the internet and have yet to find a solution. Help me Stack-Overflow-Kenobi,
I have scoured the internet for this answer, and I feel like I keep
I've scoured the internet for help on this problem but I need help. This
I have scoured the Internet for an answer to this. I have an array
I have scoured the internet looking for an answer to this question and can't
So I've been trying to solve this problem for a matter of hours now.
I have scoured the internet but can't find an answer to this : I'm
I have scoured the internet to find which CMS's run on CloudSites and the

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.