0

I've a gridview with selected columns and rows. The row consists of Textbox for every column in a row. I need to select a row in order to get the current rowindex which I've done using below code.

protected void gvtotqty_onrowdatabound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvtotqty, "Select$" + e.Row.RowIndex);
        e.Row.ToolTip = "Click to select this row.";
    }
}

protected void gvtotqty_onselectedindexchanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in gvtotqty.Rows)
    {
        if (row.RowIndex == gvtotqty.SelectedIndex)
        {
            Session["rowindex"]=row.RowIndex;
            row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
            row.ToolTip = string.Empty;
        }
        else
        {
            row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
            row.ToolTip = "Click to select this row.";
        }
    }
}

Everything is working fine where I get my rowindex when debugged but I am not able to type anything in Textbox as it is getting refreshed. I know that

Page.ClientScript.GetPostBackClientHyperlink 

calls PostBackEvent. By using this method, how can I type the values in Textboxes?

SRIRAM RAMACHANDRAN
  • 297
  • 3
  • 8
  • 23

2 Answers2

1

Try with this line of code

e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink((Control)sender, "Select$" + e.Row.RowIndex); 

instead of your

e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvtotqty, "Select$" + e.Row.RowIndex);

if it doesn't work try putting your grid inside an updatePanel and add the trigger for selected index change of your grid

<asp:UpdatePanel ID="uppan1" runat="server">
    <ContentTemplate>
       //yourGridView here with id=GridView1
    </ContentTemplate>
    <Triggers>
       <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="selectedIndexChanged"/>
    </Triggers>
</asp:UpdatePanel>

and change your selected index change function in this way

protected void gvtotqty_onselectedindexchanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in gvtotqty.Rows)
    {
        if (row.RowIndex == gvtotqty.SelectedIndex)
        {
            Session["rowindex"]=row.RowIndex;
            row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
            row.ToolTip = string.Empty;
            row.Attributes["onclick"] = "";

        }
        else
        {
            row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
            row.ToolTip = "Click to select this row.";
            row.Attributes["onclick"] =     Page.ClientScript.GetPostBackClientHyperlink(gvtotqty, "Select$" + row.RowIndex);
        }
    }
}
faby
  • 7,394
  • 3
  • 27
  • 44
  • same condition... doesn't work ... getting refresh when I clicked on textbox to type... – SRIRAM RAMACHANDRAN Jul 21 '14 at 10:53
  • @user3759652 I've update my answer. Let me know if it helps you and if you need more support – faby Jul 21 '14 at 11:15
  • I've already used UpdatePanel and how to use triggers for selected index changed?? – SRIRAM RAMACHANDRAN Jul 21 '14 at 11:18
  • I found the logic that, whenever I click the row: the onclick function is executed which was expected. But when I click the Textbox to type something which calls the same onclick which refresh the row. So, the expected logic again is: if the row is already selected, onclick should not be invoked?? thinking to find the logic.. – SRIRAM RAMACHANDRAN Jul 21 '14 at 11:23
  • Could not find an event named 'gvtotqty_onselectedindexchanged' on associated control 'gvtotqty' for the trigger in UpdatePanel 'upd' – SRIRAM RAMACHANDRAN Jul 21 '14 at 11:29
  • did you add the attribute `onselectedindexchanged="name function"` in your GridView (in trigger set `EventName="SelectedIndexChanged"`)? – faby Jul 21 '14 at 11:33
  • error goes off but it still refreshes the Text Box when I click the row. Need to write another logic I hope? – SRIRAM RAMACHANDRAN Jul 21 '14 at 11:38
  • are you binding the grid in `PageLoad` method? if yes do it only in `if !(Page.IsPostback)` – faby Jul 21 '14 at 11:41
  • try to bind the grid always... remove that if and let me know – faby Jul 21 '14 at 14:58
  • same thing happens... Thats what the above code is... Whenever I click the row, the row refreshes even when I click the TB to type something... So the logic should be... If the row is selected and if it tried to be selected, the onclick should not invoke ... – SRIRAM RAMACHANDRAN Jul 21 '14 at 15:10
  • U deserve it... thanks for helping me .... I could enter the values in TB ... let u know whether I could work completely – SRIRAM RAMACHANDRAN Jul 21 '14 at 15:32
  • happy to help you! let me know if you need more help. And remember to mark my answer as accepted to avoid that the question remains unanswered (only if my answer solve your problem) – faby Jul 21 '14 at 15:36
  • Can you help me with this logic in SQL.. http://stackoverflow.com/questions/24879020/how-to-pass-string-array-in-sql-parameter-to-in-clause-in-sql – SRIRAM RAMACHANDRAN Jul 22 '14 at 04:38
  • @SRIRAMRAMACHANDRAN I saw that you has accepted already an answer. Do you need more help? – faby Jul 22 '14 at 06:07
  • 1
    Adding the explicit AsyncPostbackTrigger to the UpdatePanel did the trick for me. Great suggestion. – Mat May 22 '18 at 16:03
0

As from your code I understand that you want to select the row and display the row in different color on selectedindex changed. If I am correct you can achieve this using the following method.

Add a button field, or template field, which contains a button with the command name "Select". Command name should be select.

<asp:ButtonField CommandName="Select"  Text="Select"/>

Then for the grid view add SelectedRowStyle

<SelectedRowStyle BackColor="#A1DCF2" />

This should already select the row with differnt color. You dont need to write code for gvtotqty_onselectedindexchanged and gvtotqty_onrowdatabound

UPDATE

Based on your comment, since you may have multiple controls on the row, like textbox, link etc. then eventhough click the textbox to enter text, it does the postback and clears the content of the text. I think you can use JavaScript for selecting row, instead of posting back as below.

<script type="text/javascript">
        function selectRow(control) {
            //Clear all the currently selected rows. change the scope, if you have multiple tables
            $('tr').css("background-color", "#FFFFFF");
            // Select the current row
            $(control).css("background-color", "#A1DCF2");
        }
</script>

Then add this function to the row in your onrowdatabound event

protected void gvtotqty_onrowdatabound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onclick"] = "selectRow(this);";
            e.Row.ToolTip = "Click to select this row.";
        }

    }
Kiran Hegde
  • 3,651
  • 1
  • 16
  • 14
  • No, I just need to know which row I typing the values in Textbox. If I am typing in 5th row, I need to select that row in some colors. That's all – SRIRAM RAMACHANDRAN Jul 21 '14 at 11:40