Link to home
Start Free TrialLog in
Avatar of ayha1999
ayha1999

asked on

GridView select row

Hi,

Im trying to change the value of lblXVal label control by changing the value in the textbox1 control (by firing textbox_changed event). When I change the value in the textbox its changes values in all lblXval controls in the grid instead of changing the lblXval for the current row where I changed the TextBox1 value.

		  <asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="False" BackColor="White"
			BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
			GridLines="Vertical" OnRowDeleting="Delete" DataKeyNames="id" Width="682px">
			<AlternatingRowStyle BackColor="#CCCCCC" />
			<Columns>
			   <asp:TemplateField HeaderText="X" SortExpression="xval">
				 <ItemTemplate>
				    <asp:Label ID="lblXVal" runat="server" Text='<%# Bind("xval") %>'></asp:Label>
				 </ItemTemplate>
			   </asp:TemplateField>

			   <asp:TemplateField HeaderText="Scale">
				 <ItemTemplate>
				    <asp:TextBox ID="TextBox1" runat="server" Text="1" AutoPostBack="true" ontextchanged="TextBox2_TextChanged"></asp:TextBox>
				 </ItemTemplate>
			   </asp:TemplateField>
...

code
 protected void TextBox2_TextChanged (object sender, EventArgs e)
   {

	 for (int count = 0 ; count < gvFiles.Rows.Count ; count++)
	 {
	    string x = ((Label)gvFiles.Rows[count].FindControl ("lblXVal")).Text;
	    string s = ((TextBox)gvFiles.Rows[count].FindControl ("TextBox1")).Text;

	    Label t = ((Label)gvFiles.Rows[count].FindControl ("lblXVal"));

	    t.Text = (Convert.ToDouble (x) * Convert.ToDouble (s)).ToString ();

	 }

}

Open in new window


How can I fix the the problem?


Ayha
Avatar of Cluskitt
Cluskitt
Flag of Portugal image

You're looping all rows. Instead of using for, just run it once and use e.RowIndex as the row number.
Avatar of ayha1999
ayha1999

ASKER

how can I RowIndex? please give me the code.

Thanks
Avatar of jitendra patil
Hi ayha1999

you need to make some changes in aspx code as below:

1. add a javascript in the head section of your page
<script type="text/javascript" language="javascript">

        function DoPostBackWithRowIndex(rowIndex){
            if (document.getElementById('<%=HDVal.ClientID%>') != null) {
                document.getElementById('<%=HDVal.ClientID%>').value = rowIndex;
            }
            return true;
        }
    </script>
2.add a hidden field above your grid view as
<asp:HiddenField ID="HDVal" runat="server" />

3. Now add a row databound event to your gridview and do the code as follows:
 protected void gvFiles_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox TxtMarks = (TextBox)e.Row.FindControl("TextBox1");
                TxtMarks.Attributes.Add("onkeydown", "javascript:return DoPostBackWithRowIndex('" + e.Row.RowIndex + "');");
            }
        }
4 .Now modify your Textchanged event as below
protected void TextBox2_TextChanged(object sender, EventArgs e)
        {
            if (HDVal.Value.Trim().Length == 0)
            {
                return;
            }
            else
            {
                int Rowindex= Convert.ToInt32(HDVal.Value);    
                string x = ((Label)gvFiles.Rows[Convert.ToInt16(Rowindex)].FindControl("lblXVal")).Text;
                string s = ((TextBox)gvFiles.Rows[Rowindex].FindControl("TextBox1")).Text;

                Label t = ((Label)gvFiles.Rows[Rowindex].FindControl("lblXVal"));
                t.Text = (Convert.ToDouble(x) * Convert.ToDouble(s)).ToString();
            }
        }
and it will work as expected.

hope this helps.
Hi patil786,

I tried followed you code exacty, it is firing but not changing the value of lblXVal.

Please check.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of jitendra patil
jitendra patil
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much.

ayha
Hi patil,

Could you please have look the following question?

https://www.experts-exchange.com/questions/28161806/Datalist-selected-row.html

Thanks

ayha