2014年7月13日 星期日

[RESOLVED] Text changed event of textbox which is inside the gridview is not working


I hvae a gridview and inside that i have added a textbox intially when grid will load it will not have


any data. After grid loading  if  i m editing the text box I want to preserve(want to save in Viewstate) the value of this textbox .


But when i m editing the particular row's textbox then  textchanged event is not invoking but after changing one textbox if i am clicking on another text box then it will invoke. So due to this the value which i had edited in first textbox is not preserving.



<asp:GridView
ID="Gridview1"
runat="server"
AutoGenerateColumns="false"
ShowFooter="true"
Height="123px"
Width



="503px">





<Columns>






<asp:TemplateField
HeaderText



="Item">





<ItemTemplate



>





<asp:DropDownList
ID="ddItm1"
runat="server"
DataTextField
="ItemDescription"
AutoPostBack="true"
DataValueField
="ItemDetailId"






OnSelectedIndexChanged
=
"ItemChanged"
AppendDataBoundItems
=
"true"
DataSourceID
="SqlDataSource1"



>





<asp:ListItem
Text="
--Select Item--"

>asp:ListItem



>





asp:DropDownList



>





ItemTemplate



>






<EditItemTemplate



>






EditItemTemplate



>





asp:TemplateField



>





<asp:TemplateField
HeaderText



="UOM">





<ItemTemplate



>





<asp:TextBox
ID="TextBox2"
runat="server">asp:TextBox



>





ItemTemplate



>





asp:TemplateField



>





<asp:TemplateField
HeaderText



="Price">





<ItemTemplate



>





<asp:TextBox
ID="TextBox3"
runat="server">asp:TextBox



>





ItemTemplate



>





asp:TemplateField



>





<asp:TemplateField
HeaderText="Qty"



>





<ItemTemplate



>





<asp:TextBox
ID="TextBox4"
OnTextChanged="QtyChanged"
AutoPostBack="true"
runat="server">asp:TextBox



>





ItemTemplate



>





asp:TemplateField



>






<asp:TemplateField
HeaderText



="Date">





<ItemTemplate



>





<asp:TextBox
ID="TextBox5"
runat="server">asp:TextBox



>





ItemTemplate



>





<FooterStyle
HorizontalAlign="Right"



/>





<FooterTemplate



>





<asp:Button
ID="btnAdd"
runat="server"
OnClick="btnAdd_Click"
Text="Add
Item"



/>





FooterTemplate>






asp:TemplateField



>






Columns


asp:GridView>


C#
Code



protected
void
QtyChanged(
object
sender,
EventArgs


e)


{








//Get the button that raised the event





TextBox
TextBox4 = (
TextBox


)sender;









//Get the row that contains this button





GridViewRow
gvr = (
GridViewRow


)TextBox4.NamingContainer;







//Get rowindex





int


rowindex1 = gvr.RowIndex ;





DataTable
dtCurrent4 = (
DataTable)
ViewState[
"CurrentTable"


];


dtCurrent4.Rows[rowindex1][




"Qty"]
=
Convert.ToInt32(Gridview1.Rows[rowindex1].Cells[3].Text);



//Here grid will not any value




ViewState[


"CurrentTable"


] = dtCurrent4;


}




 





Hi mukush,


Based on your description, I have created a sample, and it can work fine, please to refer to the following code:


In the aspx file:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TextBoxEventNotWorkingInGridView.WebForm1" %>






































AutoPostBack="true">


























In the aspx.cs code:


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TextBoxEventNotWorkingInGridView
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{

BindUsers();

}

}

///
/// Event to handle the Marks Text Box text change event.
///
///
///
protected void TxtMarksTextChanged(object sender, EventArgs eventArgs)
{

//Check whether the Hiddenfield has some value or not, if not do not move forward

if (HdnSelectedRowIndex.Value.Trim().Length == 0)
{

return;

}

//Sender will be the text box which raised the postback event

TextBox TxtMarks = (TextBox)sender;
//Find the destination textbox to which you want to copy the value from sender.
//We need to find the row index of the grid in which the value got changed, this rowindex is stored in the hiddenfield. (Javascript method will store the RowIndex on hidden field).
TextBox TxtTotal = (TextBox)GvUsers.Rows[Convert.ToInt32(HdnSelectedRowIndex.Value)].FindControl("TxtTotal");

if (TxtMarks != null && TxtTotal != null)
{

TxtTotal.Text = TxtMarks.Text;

}
//Empty the hiddenfield value once you perform the action

HdnSelectedRowIndex.Value = string.Empty;

}
///
/// Event to handle the Users Grid DataBound Event
/// This method adds a javascript method to marks text box onkeydown, that javascript method takes the rowindex as argument.
///
///
///
protected void GvUsersRowDataBound(object sender, GridViewRowEventArgs eventArgs)
{

if (eventArgs.Row.RowType == DataControlRowType.DataRow)
{
TextBox TxtMarks = (TextBox)eventArgs.Row.FindControl("TxtMarks");
TxtMarks.Attributes.Add("onkeydown", "javascript:return DoPostBackWithRowIndex('" + eventArgs.Row.RowIndex + "');");
}
}
///
/// Bind Users Grid With some sample data.
///
private void BindUsers()
{
try
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Columns.Add("Mark");
dt.Columns.Add("Total");
for (int rowindex = 1; rowindex <= 5; rowindex++)
{
dt.Rows.Add(dt.NewRow());
dt.Rows[dt.Rows.Count - 1]["ID"] = rowindex;
dt.Rows[dt.Rows.Count - 1]["FirstName"] = "FirstName" + rowindex;

dt.Rows[dt.Rows.Count - 1]["LastName"] = "LastName" + rowindex;

dt.Rows[dt.Rows.Count - 1]["Mark"] = rowindex;

dt.Rows[dt.Rows.Count - 1]["Total"] = rowindex;

}
GvUsers.DataSource = dt;

GvUsers.DataBind();

}

catch (Exception exception)
{

Response.Write(exception.Message);

}

}
}
}

Hope it can help you.


If you have any question please let me know.


Best Regards,

Terry Guo



Thank You very much ,


Your code helped me a lot i applied and it is working but i want to change another cell of


 grid when i m changing in textbox which is inside the same grid.


 


 


沒有留言:

張貼留言