locked
ASP.Net Grid View Question RRS feed

  • 問題

  • Hi

    I have a page which has a Grid view to display some records and allow them to make changes on it. On the grid view I has several Textboxs and Label and a check box.
    The changes can be make via a Edit/Save Button (asp:button) and programm it to only update when the check box for the record has been checked.

    I am wondering if there any way that I could highlight a grid view row after they modified a record (a textbox's value)

    I tried to use the textbox.changes(), it does not seems to work.

    Thanks in advances.
    2008年7月14日 下午 03:45

解答

  • Hi Chi,

     

    I did a quick test and highlight a GridViewRow whenever the TextBox got focus (or being updated in your case).

     

    FYI: http://www.flickr.com/photos/coltkwong/2672607949/

     

    It is done by javascript and the code is:

     

    Code Snippet

    <script type="text/javascript">
        function ChangeColor()
        {
            var obj = window.event.srcElement;

            if(obj.tagName == 'INPUT' && obj.type == 'text')
            {
                obj = obj.parentElement.parentElement;
                obj.style.background = 'orange';
            }
        }
    </script>

     

    <asp:TemplateField>
        <ItemTemplate>
            <asp:TextBox ID="txtColor" runat="Server" onfocus="java-script:ChangeColor();"/>
        </ItemTemplate>                   
    </asp:TemplateField>

     

     

    Of course, you can add some NULL checking in the code, and revert the background color to normal if users undo his changes in GridView.

     

    Regards,

    Colt

    2008年7月16日 上午 03:45
  • Hi Chi,

     

    You're welcome. Please mark this post as useful if you find it is useful.

    It's fine to place the javascript in either place, and personally I'd like to put it on aspx page or on a separate .js file.

     

    Regards,

    Colt

    2008年7月16日 上午 09:30

所有回覆

  • Hi Chi,

     

    Once you click the Save button, can you loop the rows in GridView and determine the status of CheckBox?

     

    E.g.

     

    Code Snippet
    foreach (GridViewRow r in this.GridView1.Rows)
    {
        CheckBox cb = (CheckBox)r.FindControl("CheckBox1");
        if (cb.Checked) { XXX }
    }

     

     

    Regards,
    Colt

    2008年7月15日 上午 02:15
  • Hi Colt,

    Thanks for your information. I am sorry that I did not explain it properly.

    What I am trying to do is to highlight the row after the textbox which is on the grid view get changes. Because I would like to let the use know which rows they have modified, so that they can decide whether they want to check the checkbox.

    Sorry to make you confused.
    2008年7月15日 上午 07:23
  • Hi Chi,

     

    Can you post your code of GridView, are you using EditItemTemplate or something else? It sounds like you better do what you want in JavaScript rather than at server side code.

     

    Regards,

    Colt

    2008年7月16日 上午 01:50
  • Hi Chi,

     

    I did a quick test and highlight a GridViewRow whenever the TextBox got focus (or being updated in your case).

     

    FYI: http://www.flickr.com/photos/coltkwong/2672607949/

     

    It is done by javascript and the code is:

     

    Code Snippet

    <script type="text/javascript">
        function ChangeColor()
        {
            var obj = window.event.srcElement;

            if(obj.tagName == 'INPUT' && obj.type == 'text')
            {
                obj = obj.parentElement.parentElement;
                obj.style.background = 'orange';
            }
        }
    </script>

     

    <asp:TemplateField>
        <ItemTemplate>
            <asp:TextBox ID="txtColor" runat="Server" onfocus="java-script:ChangeColor();"/>
        </ItemTemplate>                   
    </asp:TemplateField>

     

     

    Of course, you can add some NULL checking in the code, and revert the background color to normal if users undo his changes in GridView.

     

    Regards,

    Colt

    2008年7月16日 上午 03:45
  • Hi Colt

    Thanks very much for your help, you are amazing. It works very well. I did not use Edit Template for it, I do not know why I have a bade habit that I love to type the code and program it rather than using Object data source ... etc.

    I have one more question, about the Javascript.
    Do you recommend me to register it on Page Load event? or just place it on aspx page?

    Thanks a lots

    2008年7月16日 上午 07:51
  • Hi Chi,

     

    You're welcome. Please mark this post as useful if you find it is useful.

    It's fine to place the javascript in either place, and personally I'd like to put it on aspx page or on a separate .js file.

     

    Regards,

    Colt

    2008年7月16日 上午 09:30
  • Hi Colt

    I have another problem, about highlight a row on Grid View. After I click on the submit button, the highlighted row has back to normal. Is there any way that I could keep the row highlighted, after clicking on the submit button?

    Thanks in advance.
    2008年8月1日 下午 03:29