If I want to access the method in which we define update query, how can I access that method on aspx page & update the data in a grid view without direct use of an query on aspx page under sql data source?
28 6666
What you would have to do is implement a method that handles the GridView.RowUpdating Event. In this method you will have to retrieve the data from the row that is updating and use this data to update your database.
I'm not sure what you're stuck on so if you provide more information I can give you more help on the topic.
-Frinny
@Frinavale
sorry frinny for my titile i use......
Actually I made a data acces layer class, buiness logic layer class & presentation layer(.aspx page). Now I want to update the data in a grid view.
In most of the books generally direct update query is written on aspx page under sql data source using update commnad property & do updation in grid view.
Here is the code -
<asp:SqlDataSource ID="datasource1" ProviderName="System.Data.SqlClient" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString%>" SelectCommand="Select * From ManageClient"
-
UpdateCommand="Update ManageClient Set ClientName=@ClientName,Address=@Address,MobileNo=@MobileNo,Image=@Image Where ClientId=@ClientId"></asp:SqlDataSource>
-
Connect it to the grid view.
Now i do not want to use update command or connection string in sql data source.
This thing I have done in data acces layer class where I make a method of an update.
I want to use this method so I can do an update in grid view directly.
I've never actually used the SqlDataSource before. I like to be in more control of my database connections.
Anyways, it's not that hard to do. Delete your SqlDataSource...you don't need it. You don't need to use a SqlDataSource at all. GridViews can bind to DataTables, DataViews, Arrays of Objects ...and more.
So, you need to populate the object that you are going to use as your data source at some point in your page life cycle. You could do this in your Page Load event if you want to.
You could cache the object that you are going to use as a data source too. This way you don't have to repopulate the data source every page postback. This saves time but it depends on how long you can tolerate "old data" being displayed.
Anyways, once you've populated your data source (or retrieved it from cache) you set the GridView.DataSource property to the object you are using...then call the GridView.DataBind method to bind the GridView to the data source. I do this in the Page PreRender event for a bunch of reasons....for example if you bind in the Page Load event every page request you will have problems with getting the values that the user entered while editing (because these values will be over written with the data that you've bound to).
In your GridViews OnRowUpdating event you will call your data access layer's methods to do the updating....
That's pretty much it.
-Frinny
@Frinavale
You said that we have to bind object with grid view at page.prerender . I think if we do the same thing on page load event it work because value is set when row updating event is fired. When this event fired it saves the values at their view state & this view state is sent to the client. That is the concept of page life cycle. Am I right?
Not quite right.
What happens is that the ViewState for the GridView is loaded just before the Page Load event. This means that all of the data that the user provided for the row that they are updating is loaded into the ASP.NET controls used to access the data in your C#/VB.NET code.
If you do a DataBind in the Page Load event then all of this data is lost! Edit: if you do a DataBind in the Page Load event every page request all of your data will be lost
It's the most common problem that people have with GridViews that I've noticed.
If you don't need to get data from the user, than yes you can do the DataBind in the Page Load event. But if you need to get data from the user for updating purposes then, no, you should do it in the Page PreRender event instead.
-Frinny
@Frinavale
The event is fired between load view state & save view state when the event is fired it set the values in textbox or whatever server control we used. After that it saves the values in view state. Then there is no matter of data loss if you load the page again but I have to bind data in a grid view through view state on page load no chance of data loss.
You are mistaken.
Events are created when the ViewState is loaded. Methods that handle events occur after the Page Load event.
This is the basic outline of what happens in the ASP.NET Life Cycle: - Page Init (before ViewState is loaded)
- Page Load (after ViewState is loaded)
- Event handling code is executed
- Page PreRender (occurs Just before the page is rendered as HTML)
Please review the ASP.NET Page Life Cycle for a more detailed explination.
If you perform a GridView.DataBind in the Page Load event (without checking if it's postback) then you will not be able to retrieve user input in the GridView.RowEditing event.
-Frinny
@Frinavale
Can you tell me when the data is saved in a view state & that data is loss from the view state next time when it load the view state?
The ViewState is stored some time in between the Page PreRender event and the SaveStateComplete event (heading towards the Render stage).
All of this is covered in the article I sent you a link to :)
I'm not sure what you mean by:
AnagJohari: that data is loss from the view state
Maybe you're talking about data loss that happens if you don't call the DataBind method??
@Frinavale
Now i implemented the editing in gridview i handle events & after that i call display method in which i bind the grid view on page load event its work correctly.......
i want to say one more thing i call the display method at the end of every event which i handled ....
like onrowupdating event after performing all operation
i call display method at the end.
you also check this link please , by thw way ur link is not working. http://msdn.microsoft.com/en-us/library/ms972976.aspx
I've fixed the link.
I'm sorry AnagJohari, but you've completely lost me.
I'm am fully aware of when ViewState is loaded....it's loaded right after the Page Init Event and before Page Load event.
So, the ViewState with all of the data for your GridView is loaded just after the Init event. This makes the data in your GridView is available in the Page Load event...including data entered by the user.
If you do a DataBind in your Page Load event every page request (without checking if isPostback is False) then you will be overwritting anything that the user provided.
BUT this information that I'm telling you does not matter to you because you don't care about anything the user has entered into the GridView for editing. (Well it should matter to you because this is a very important behaviour that you need to understand)
All you want to do is determine which row the user was editing (which is easily done in the OnRowEditing event)...and retrieve any information that you need in order to display the information in more detail in some other "page" or "control".
You were not very clear about this.
This is why you are able to do a DataBind in your Page Load without losing data entered by the user.
Anyways.
I don't know what you're talking about with regards to your "display" method.
I'm not even sure you have a question at this point... :)
-Frinny
@Frinavale
Frinny I send You a code ,
By the way You are so senior & i respect each & every word which you are writing for me....
I am just new ,so i m eager to know what is the problem actually .In mycode,
why i donot lose my data in grid view.
I just need your help Thats it -
That is .cs code file
-
-
using System;
-
using System.Collections;
-
using System.Configuration;
-
using System.Data;
-
using System.Linq;
-
using System.Web;
-
using System.Web.Security;
-
using System.Web.UI;
-
using System.Web.UI.HtmlControls;
-
using System.Web.UI.WebControls;
-
using System.Web.UI.WebControls.WebParts;
-
using System.Xml.Linq;
-
using System.Collections.Generic;
-
using BllDummy;
-
-
public partial class ManageClient : System.Web.UI.Page
-
{
-
DummyBLL dummy = new DummyBLL();
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
if (!Page.IsPostBack)
-
{
-
ddlbind();
-
display();
-
}
-
}
-
protected void click_Add(object sender, EventArgs e)
-
{
-
dummy.ClientName = txtClientName.Text;
-
dummy.Address = txtAddress.Text;
-
dummy.MobileNo = Convert.ToInt64(txtMobileNo.Text);
-
dummy.Image = txtImage.Text;
-
dummy.add();
-
txtAddress.Text = string.Empty;
-
txtClientName.Text = string.Empty;
-
txtImage.Text = string.Empty;
-
txtMobileNo.Text = string.Empty;
-
txtClientName.Text = "the addtion is done";
-
display();
-
-
}
-
private void display()
-
{
-
gv_DisplayClient.DataSource = DummyBLL.display();
-
// gv_DisplayClient.DataSourceID = "datasource1";
-
gv_DisplayClient.DataBind();
-
}
-
private void ddlbind()
-
{
-
List<DummyBLL> list = DummyBLL.Clientlist();
-
ddlClient.Items.Add(new ListItem("--SelectClient--", "0"));
-
foreach (DummyBLL dummylist in list)
-
{
-
ddlClient.Items.Add(new ListItem(dummylist.ClientName, dummylist.ClientId.ToString()));
-
}
-
}
-
-
-
-
/*protected void ClickStatus(object sender, GridViewCommandEventArgs e)
-
{
-
if (e.CommandName == "StatusClick")
-
{
-
message.Text = "You Currently Click The ClientId=" + e.CommandArgument.ToString();
-
}
-
}*/
-
-
protected void View_Click(object sender, EventArgs e)
-
{
-
-
}
-
protected void GvEditing(object sender, GridViewEditEventArgs e)
-
{
-
gv_DisplayClient.EditIndex = e.NewEditIndex;
-
display();
-
}
-
protected void gv_EditCancel(object sender, GridViewCancelEditEventArgs e)
-
{
-
gv_DisplayClient.EditIndex = -1;
-
display();
-
}
-
-
protected void gv_Updating(object sender, GridViewUpdateEventArgs e)
-
{
-
-
Label lb = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("Label6");
-
int ClientId = Convert.ToInt32(lb.Text);
-
TextBox tx1 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvClientName");
-
TextBox tx2 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvAddress");
-
TextBox tx3 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvMobileNo");
-
TextBox tx4 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvImage");
-
dummy.ClientName = tx1.Text;
-
dummy.Address = tx2.Text;
-
dummy.MobileNo = Convert.ToInt32(tx3.Text);
-
dummy.Image = tx4.Text;
-
dummy.ClientId = ClientId;
-
dummy.Update();
-
gv_DisplayClient.EditIndex = -1;
-
display();
-
-
}
-
protected void gv_Deleting(object sender, GridViewDeleteEventArgs e)
-
{
-
Label lbl = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("Label6");
-
dummy.ClientId = Convert.ToInt32(lbl.Text);
-
dummy.Delete();
-
display();
-
}
-
-
this event i write after our discussion so its seprate thing, by the way thisevent is not working
-
at this time
-
protected void Gv_Insert(object sender, GridViewRowEventArgs e)
-
{
-
TextBox tx1 = (TextBox)gv_DisplayClient.FindControl("gvClientName");
-
TextBox tx2 = (TextBox)gv_DisplayClient.FindControl("gvAddress");
-
TextBox tx3 = (TextBox)gv_DisplayClient.FindControl("gvMobileNo");
-
TextBox tx4 = (TextBox)gv_DisplayClient.FindControl("gvImage");
-
-
dummy.ClientName = tx1.Text;
-
dummy.Address = tx2.Text;
-
dummy.MobileNo = Convert.ToInt32(tx3.Text);
-
dummy.Image = tx4.Text;
-
dummy.add();
-
display();
-
}
-
}
-
-
There is problem in this event that is gv_insert i make it in bold letter.
-
i took on rowcreating event but not able to fetch the value from grid view row
-
i want that when i click on add the dynamic row is created in editable form
-
after i enter the value in that row the data is add in database
-
.aspx code
please see my code & give your important opinion.
Thank you
This is a lot easier to understand!
Ok the reason why you aren't losing data is because you are only doing your DataBind in your Page Load event the first time the page is loaded (if !IsPostback). This way your data is not getting over written when the user-provided-edit-data is posted back to the server.
But do you notice how you are calling the display() method in several different places? Wouldn't it be easier if you just had to call it once? That is why I recommended that you do this in your Page PreRender event.
Ok so everything is looking pretty good :)
But instead of using the Insert event to try and populate your TextBoxes, try using the GridView.RowDataBound event :)
Edit: Do not use the DataRowBound event....you should be using the GridView.RowCommand event. I should have been more observant earlier to see what you were trying to do! I'm sorry.
-Frinny
@Frinavale
Now I get the answer your mean is that when we bind the data at page load event. the view state is already loaded with previous value & that value again saved in a view state so the same data is displayed again & again whether you edit the data in a grid view or not.
but if we bind the data at page.prerender event then this value is loaded in a view state & after page load event this view state is saved & the data is displayed with updated value..
Am i right frinny? please reply me that what i said is right or not.
other thing i want to know what about the load postback event that is also fire between pageprerender & page load . i read about this event in msdn but not able to distinquish beacuse loadpostback event is also use to remember the value & pageprerender also for remebering the values.
thank you
Now I get the anser your mean is that when we bind the data at page load event. the view state is already loaded with previous value & that value gain saved in a view state so the same data is displayed again & again whether you edit the data in a grid view or not.
:)
but if we bind the data at page.prerender event then this value is loaded in a view state & after page load event this view state is saved & the data is displayed with updated value..
Am i right frinny? please reply me that what i said is right or not.
The point I was trying to make is that you should be binding to your GridView after you have finished manipulating the data within it.....after the row had been deleted or edited or added. The PreRender event occurs after everything is finished executing and is sometimes a good place to do this.
You are doing things just fine the way you are.
I was recommending doing it in the PreRender event for simplicity sake but after playing around with a your GridView and my own little custom DummyBLL class...I am starting to think that your current technique is better than my suggestion!
:)
other thing i want to know what about the load postback event that is also fire between pageprerender & page load . i read about this event in msdn but not able to distinquish beacuse loadpostback event is also use to remember the value & pageprerender also for remebering the values.
Which article were you reading?
One thing I noticed when I took your took your GridView (that you posted) is that your DummyBLL class has a property "ClientID".
"ClientID" has a special meaning in ASP.NET controls...it is the ID that is given to the HTML element that represents the control in the browser.
You should consider renaming this property to avoid confusion.
This is what I used while testing your code:
GridView (ASPX)...notice how I changed the ClientID to DummyID: -
<asp:GridView ID="gv_DisplayClient" runat="server" DataKeyNames="DummyID" ForeColor="#333333"
-
GridLines="Horizontal" CellPadding="4" AutoGenerateColumns="false" OnRowEditing="GvEditing"
-
OnRowCancelingEdit="gv_EditCancel" EnableViewState="true" OnRowUpdating="gv_Updating"
-
OnRowDeleting="gv_Deleting">
-
<RowStyle BackColor="#F7F6F3" ForeColor="#000000" />
-
<HeaderStyle BackColor="BurlyWood" ForeColor="AliceBlue" Font-Bold="true" />
-
<Columns>
-
<%-- <asp:BoundField DataField="ClientId" ReadOnly="true" HeaderText="ClientId" />
-
<asp:BoundField DataField="ClientName" HeaderText="Client Name" />
-
<asp:BoundField DataField="Address" HeaderText="Address" />
-
<asp:BoundField DataField="MobileNo" HeaderText="MobileNo" />
-
<asp:ImageField DataImageUrlField="Image" HeaderText="Image"/>
-
<asp:CommandField ShowEditButton="true" SelectText="Edit"/>
-
<asp:TemplateField HeaderText="click">
-
<ItemTemplate>
-
<asp:Button ID="click" runat="server" CommandName="StatusClick" CommandArgument='<%#Eval("ClientId")%>' Text="Click" />
-
-
</ItemTemplate>
-
-
</asp:TemplateField>--%>
-
<asp:TemplateField HeaderText="ClientId">
-
<ItemTemplate>
-
<asp:Label ID="Label6" runat="server" Text='<%#Bind("DummyID")%>'></asp:Label>
-
</ItemTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField HeaderText="ClientName">
-
<ItemTemplate>
-
<asp:Label ID="lblClientName" runat="server" Text='<%#Eval("ClientName")%>'></asp:Label>
-
</ItemTemplate>
-
<EditItemTemplate>
-
<asp:TextBox ID="gvClientName" runat="server" Text='<%#Bind("ClientName")%>'></asp:TextBox>
-
</EditItemTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField HeaderText="Address">
-
<ItemTemplate>
-
<asp:Label ID="lblAddress" runat="server" Text='<%#Eval("Address")%>'></asp:Label>
-
</ItemTemplate>
-
<EditItemTemplate>
-
<asp:TextBox ID="gvAddress" runat="server" Text='<%#Bind("Address")%>'></asp:TextBox>
-
</EditItemTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField HeaderText="MobileNo">
-
<ItemTemplate>
-
<asp:Label ID="lblMobileNo" runat="server" Text='<%#Eval("MobileNo")%>'></asp:Label>
-
</ItemTemplate>
-
<EditItemTemplate>
-
<asp:TextBox ID="gvMobileNo" runat="server" Text='<%#Bind("MobileNo")%>'></asp:TextBox>
-
</EditItemTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField HeaderText="Image">
-
<ItemTemplate>
-
<asp:Label ID="lblImage" runat="server" Text='<%#Eval("Image")%>'></asp:Label>
-
</ItemTemplate>
-
<EditItemTemplate>
-
<asp:TextBox ID="gvImage" runat="server" Text='<%#Bind("Image")%>'></asp:TextBox>
-
</EditItemTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField HeaderText="Operations">
-
<ItemTemplate>
-
<asp:LinkButton ID="edit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
-
<asp:LinkButton ID="Delete" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
-
</ItemTemplate>
-
<EditItemTemplate>
-
<asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
-
<asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
-
<asp:LinkButton ID="btnAdd" runat="server" CommandName="Insert" Text="Insert"></asp:LinkButton>
-
</EditItemTemplate>
-
</asp:TemplateField>
-
</Columns>
-
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
-
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
-
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
-
<EditRowStyle BackColor="#999999" />
-
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
-
</asp:GridView>
C#: -
protected void Page_Load(object sender, EventArgs e)
-
{
-
if (IsPostBack == false)
-
{
-
gv_DisplayClient.DataSource = DummyBLL.display();
-
gv_DisplayClient.DataBind();
-
}
-
}
-
-
-
protected void GvEditing(object sender, GridViewEditEventArgs e)
-
{
-
gv_DisplayClient.EditIndex = e.NewEditIndex;
-
display();
-
}
-
-
protected void gv_EditCancel(object sender, GridViewCancelEditEventArgs e)
-
{
-
gv_DisplayClient.EditIndex = -1;
-
display();
-
}
-
-
-
protected void gv_Updating(object sender, GridViewUpdateEventArgs e)
-
{
-
-
Label lb = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("Label6");
-
int ClientId = Convert.ToInt32(lb.Text);
-
TextBox tx1 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvClientName");
-
TextBox tx2 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvAddress");
-
TextBox tx3 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvMobileNo");
-
TextBox tx4 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvImage");
-
DummyBLL dummy = new DummyBLL();
-
dummy.ClientName = tx1.Text;
-
dummy.Address = tx2.Text;
-
dummy.MobileNo = tx3.Text;
-
dummy.Image = tx4.Text;
-
dummy.DummyID = ClientId;
-
DummyBLL.update(dummy);
-
gv_DisplayClient.EditIndex = -1;
-
display();
-
-
}
-
-
protected void gv_Deleting(object sender, GridViewDeleteEventArgs e)
-
{
-
Label lbl = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("Label6");
-
-
Int32 dummyID = Convert.ToInt32(lbl.Text);
-
DummyBLL.delete(dummyID);
-
display();
-
}
-
-
private void display() {
-
gv_DisplayClient.DataSource = DummyBLL.display();
-
gv_DisplayClient.DataBind();
-
}
And this is my custom DummyBLL class (Please note that I don't have a database to connect to so I'm using a List<DummyBLL> which is stored in Session instead.): -
-
private class DummyBLL
-
{
-
private static List<DummyBLL> _source;
-
private Int32 _id;
-
private String _name;
-
private String _address;
-
private String _mobileNo;
-
private String _image;
-
-
public Int32 DummyID
-
{
-
get { return _id; }
-
set { _id = value; }
-
}
-
public String ClientName
-
{
-
get { return _name; }
-
set { _name = value; }
-
}
-
public String Address
-
{
-
get { return _address; }
-
set { _address = value; }
-
}
-
public String MobileNo
-
{
-
get { return _mobileNo; }
-
set { _mobileNo = value; }
-
}
-
public String Image
-
{
-
get { return _image; }
-
set { _image = value; }
-
}
-
-
public static List<DummyBLL> display()
-
{
-
List<DummyBLL> source = new List<DummyBLL>();
-
if (HttpContext.Current.Session["_source"] != null)
-
{
-
_source = (List<DummyBLL>)HttpContext.Current.Session["_source"];
-
source = _source;
-
}
-
else
-
{
-
String[] names = { "Bob", "Sally", "Frank", "Steve", "Beth" };
-
-
int i = 0;
-
foreach (string name in names)
-
{
-
source.Add(new DummyBLL(i, name, name + "'s address", "1-999-333-222" + i.ToString(), name + "'s picture"));
-
i++;
-
}
-
}
-
_source = source;
-
HttpContext.Current.Session["_source"] = _source;
-
return source;
-
}
-
public DummyBLL()
-
{
-
_id = 0;
-
_name = "";
-
_address = "";
-
_mobileNo = "";
-
_image = "";
-
}
-
public DummyBLL(Int32 id, String name, String address, String mobileNo, String imgName)
-
{
-
_id = id;
-
_name = name;
-
_address = address;
-
_mobileNo = mobileNo;
-
_image = imgName;
-
}
-
public static void add(DummyBLL newDummyBLL)
-
{
-
_source = display();
-
newDummyBLL.DummyID = _source.Count + 1;
-
_source.Add(newDummyBLL);
-
HttpContext.Current.Session["_source"] = _source;
-
}
-
public static void delete(Int32 id)
-
{
-
_source = display();
-
DummyBLL toDelete = Array.Find<DummyBLL>(_source.ToArray(), (x) => x.DummyID == id);
-
if (toDelete != null)
-
{
-
_source.Remove(toDelete);
-
}
-
HttpContext.Current.Session["_source"] = _source;
-
}
-
public static void update(DummyBLL updatedDummy)
-
{
-
Int32 id = updatedDummy.DummyID;
-
_source = display();
-
DummyBLL toDelete = Array.Find<DummyBLL>(_source.ToArray(), (x) => x.DummyID == id);
-
if (toDelete != null)
-
{
-
_source.Remove(toDelete);
-
_source.Add(updatedDummy);
-
}
-
HttpContext.Current.Session["_source"] = _source;
-
}
-
-
}
-Frinny
Now that I have some code to see and work with I think I understand your problem a Lot better now.
You want to be able to let the user insert a row with a new DummyBLL record.
You've added an "insert button" link button into the EditTemplate for each row and you can't figure out how to handle this.
I'm sorry but the RowDataBound event that I recommended earlier will not work. At that point I hadn't take a serious look at your code.
You want to use the GridView.RowCommand Event.
This Event is thrown every time a control within the GridView causes a post back to the server. You can determine which control it was using the GridViewCommandEventArgs object passed in as parameter "e".
So, you should have something like: -
protected void gv_DisplayClient_RowCommand(Object sender, GridViewCommandEventArgs e)
-
{
-
if (e.CommandName == "Insert")
-
{
-
GridViewRow rowToInsert = gv_DisplayClient.Rows[gv_DisplayClient.EditIndex];
-
if (rowToInsert != null)
-
{
-
TextBox tx1 = (TextBox)rowToInsert.FindControl("gvClientName");
-
TextBox tx2 = (TextBox)rowToInsert.FindControl("gvAddress");
-
TextBox tx3 = (TextBox)rowToInsert.FindControl("gvMobileNo");
-
TextBox tx4 = (TextBox)rowToInsert.FindControl("gvImage");
-
DummyBLL dummy = new DummyBLL();
-
dummy.ClientName = tx1.Text;
-
dummy.Address = tx2.Text;
-
dummy.MobileNo = tx3.Text;
-
dummy.Image = tx4.Text;
-
DummyBLL.add(dummy);
-
gv_DisplayClient.EditIndex = -1;
-
display();
-
}
-
}
-
}
Where you have you have your GridView definition as: -
<asp:GridView
-
ID="gv_DisplayClient"
-
runat="server"
-
DataKeyNames="DummyID"
-
ForeColor="#333333"
-
GridLines="Horizontal"
-
CellPadding="4"
-
AutoGenerateColumns="false"
-
OnRowEditing="GvEditing"
-
OnRowCancelingEdit="gv_EditCancel"
-
EnableViewState="true"
-
OnRowUpdating="gv_Updating"
-
OnRowDeleting="gv_Deleting"
-
OnRowCommand="gv_DisplayClient_RowCommand">
I personally think this is confusing
I personally find it confusing to have to click the Edit button to be able to Insert a new record...especially since when you hit the Edit button the row becomes editable and the Insert takes the values of the row you're "editing" but the row your "editing" is not effected by the insert.
I would put an insert button outside of the GridView.
Or I would add an "empty" row to the GridView...and if the user clicks on this row they will only have Insert available.
You could add this row as a "footer" or a "header"....
There's lots of options really that are less confusing to use than what you currently have.
@Frinavale
Thank You very much for writing such sort of code & giving me your precious time....
by the way i also get anew thing from your code related to session how we can work if we donot have database ,really thanks
i m not able to understand one statement .... -
DummyBLL toDelete = Array.Find<DummyBLL>(_source.ToArray(), (x) => x.DummyID == id);
-
please explain the above line
its new for me the coding style u adopt. plz explain about this also.
Thank you again
.
@AnagJohari
Actually i want to take the advantage of using add button in every row of grid view, i want that when i click on add button the new row is created at the next index on which row i clicked. so it helps to insert any recode at any position on grid view. but i donot want that effect the clientid.
suppose
you click on the add button of that row whose client id 3 and suppose index is also 3
& there are other three records downside
so total records is 6 last record in a grid view have client id 6
i want that the record of client id 7 just insert down the record of client id 3
on which i click for insert
Thank you
its looks more user friendly .
@AnagJohari
I add another thing also is that when i call display & bind function on page load event i have never losed the data . the record i edit or add display properly on grid view while i also donot use display button after every event also -
using System;
-
using System.Collections;
-
using System.Configuration;
-
using System.Data;
-
using System.Linq;
-
using System.Web;
-
using System.Web.Security;
-
using System.Web.UI;
-
using System.Web.UI.HtmlControls;
-
using System.Web.UI.WebControls;
-
using System.Web.UI.WebControls.WebParts;
-
using System.Xml.Linq;
-
using System.Collections.Generic;
-
using BllDummy;
-
-
public partial class ManageClient : System.Web.UI.Page
-
{
-
-
DummyBLL dummy = new DummyBLL();
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
-
ddlbind();
-
display();
-
-
if (!Page.IsPostBack)
-
{
-
// ddlbind();
-
//display();
-
}
-
}
-
protected void click_Add(object sender, EventArgs e)
-
{
-
dummy.ClientName = txtClientName.Text;
-
dummy.Address = txtAddress.Text;
-
dummy.MobileNo = Convert.ToInt64(txtMobileNo.Text);
-
dummy.Image = txtImage.Text;
-
dummy.add();
-
txtAddress.Text = string.Empty;
-
txtClientName.Text = string.Empty;
-
txtImage.Text = string.Empty;
-
txtMobileNo.Text = string.Empty;
-
txtClientName.Text = "the addtion is done";
-
display();
-
-
}
-
private void display()
-
{
-
gv_DisplayClient.DataSource = DummyBLL.display();
-
// gv_DisplayClient.DataSourceID = "datasource1";
-
gv_DisplayClient.DataBind();
-
}
-
private void ddlbind()
-
{
-
List<DummyBLL> list = DummyBLL.Clientlist();
-
ddlClient.Items.Add(new ListItem("--SelectClient--", "0"));
-
foreach (DummyBLL dummylist in list)
-
{
-
ddlClient.Items.Add(new ListItem(dummylist.ClientName, dummylist.ClientId.ToString()));
-
}
-
}
-
-
-
-
/*protected void ClickStatus(object sender, GridViewCommandEventArgs e)
-
{
-
if (e.CommandName == "StatusClick")
-
{
-
message.Text = "You Currently Click The ClientId=" + e.CommandArgument.ToString();
-
}
-
}*/
-
-
protected void View_Click(object sender, EventArgs e)
-
{
-
-
}
-
protected void GvEditing(object sender, GridViewEditEventArgs e)
-
{
-
gv_DisplayClient.EditIndex = e.NewEditIndex;
-
display();
-
}
-
protected void gv_EditCancel(object sender, GridViewCancelEditEventArgs e)
-
{
-
gv_DisplayClient.EditIndex = -1;
-
display();
-
}
-
-
protected void gv_Updating(object sender, GridViewUpdateEventArgs e)
-
{
-
-
Label lb = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("Label6");
-
int ClientId = Convert.ToInt32(lb.Text);
-
TextBox tx1 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvClientName");
-
TextBox tx2 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvAddress");
-
TextBox tx3 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvMobileNo");
-
TextBox tx4 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvImage");
-
dummy.ClientName = tx1.Text;
-
dummy.Address = tx2.Text;
-
dummy.MobileNo = Convert.ToInt32(tx3.Text);
-
dummy.Image = tx4.Text;
-
dummy.ClientId = ClientId;
-
dummy.Update();
-
gv_DisplayClient.EditIndex = -1;
-
// display();
-
-
}
-
-
-
protected void gv_Deleting(object sender, GridViewDeleteEventArgs e)
-
{
-
Label lbl = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("Label6");
-
dummy.ClientId = Convert.ToInt32(lbl.Text);
-
dummy.Delete();
-
// display();
-
}
-
-
-
-
-
/* protected void Gv_Insert(object sender, GridViewRowEventArgs e)
-
{
-
TextBox tx1 = (TextBox)gv_DisplayClient.FindControl("gvClientName");
-
TextBox tx2 = (TextBox)gv_DisplayClient.FindControl("gvAddress");
-
TextBox tx3 = (TextBox)gv_DisplayClient.FindControl("gvMobileNo");
-
TextBox tx4 = (TextBox)gv_DisplayClient.FindControl("gvImage");
-
-
dummy.ClientName = tx1.Text;
-
dummy.Address = tx2.Text;
-
dummy.MobileNo = Convert.ToInt32(tx3.Text);
-
dummy.Image = tx4.Text;
-
dummy.add();
-
// display();
-
}*/
-
protected void gv_Add(object sender, GridViewCommandEventArgs e)
-
{
-
{
-
if (e.CommandName == "Insert")
-
{
-
GridViewRow row = gv_DisplayClient.FooterRow;
-
if (row != null)
-
{
-
// gv_DisplayClient.EditIndex = -1;
-
TextBox tx1 = (TextBox)row.FindControl("txtaddClientName");
-
TextBox tx2 = (TextBox)row.FindControl("txtaddAddress");
-
TextBox tx3 = (TextBox)row.FindControl("txtaddMobileNo");
-
TextBox tx4 = (TextBox)row.FindControl("txtaddImage");
-
dummy.ClientName = tx1.Text;
-
dummy.Address = tx2.Text;
-
dummy.MobileNo = Convert.ToInt32(tx3.Text);
-
dummy.Image = tx4.Text;
-
dummy.add();
-
// display();
-
}
-
}
-
-
}
-
}
-
-
protected void gv_DisplayClient_PreRender(object sender, EventArgs e)
-
{
-
//ddlbind();
-
//display();
-
-
-
}
-
}
Its work fine while u said that the data is loss but data is not lost.
i am really confuse in this topic.....
I don't know why you aren't experiencing data loss.
This has been a common problem that is constantly posted about and the fix is to move what you're doing in Page Load (every time the page is loaded) to the Page PreRender event (ever time the page is about to be rendered).
Then again it might have something to do with the fact that you have specifically stated that you are enabling ViewState for the GridView....I wonder if this could be why you aren't having problems.
This statement: -
DummyBLL toDelete = Array.Find<DummyBLL>(_source.ToArray(), (x) => x.DummyID == id);
Finds the first DummyBLL object in the _source (an Array of DummyBLL objects) that has a "DummyID" property that matches the id passed in as a parameter to the method.
It uses lambda expressions to simplify things.
I remember the first time I saw code like this....it changed my coding style completely.
-Frinny
@Frinavale Actually I am mistaken data is actually loss when i call this on page load event
now its clear........
i want to take the advantage of using add button in every row of grid view, i want that when i click on add button the new row is created at the next index on which row i clicked. so it helps to insert any recode at any position on grid view. but i donot want that effect the clientid.
suppose
you click on the add button of that row whose client id 3 and suppose index is also 3
& there are other three records downside
so total records is 6 last record in a grid view have client id 6
i want that the record of client id 7 just insert down the record of client id 3
on which i click for insert
Thank you
its looks more user friendly .
Hmm interesting.
I think you will have to modify the datasource itself to allow for this...
I'll have to think more on the topic. I'm pretty busy this weekend though and probably won't be able to help you very quickly on this one....
Try inserting a New DummyBLL into your source at the spot that you want it to be at. Give it an ID and let the user enter the rest of the data before saving it to your database (or whatever data store you're using).
-Frinny
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Nigi |
last post by:
I've made a mysql database with php front end which, in part, contains
mailing lists. I've used a mailto: link to create a new email with
their names in the bcc: field.
Unfortunatley their is a...
|
by: phanimadhav |
last post by:
hi this is sudheer,
i am new one of this ASP.NET .i have one problem,i am using gridview control i know how to place the dropdownlistbox in gridview control.in my gridveiw control have contain...
|
by: jackvel |
last post by:
Hi Dudes...
I want to connect the Access database with VB 6.0 without using DataControl and ADODC.. Which option should i use.? Plz send me the detailed procedure of doing the same.. Plz...
|
by: mathewgk80 |
last post by:
Hi all,
I would like to connect gridview with database without using sql datasource..
I am using sql server and c#.net..
Please help me..
regards,
Mathew.
|
by: jaz215 |
last post by:
hi! how do i add a record in a database without using the datacontrol and adodc. i want to rework my code to using purely codes and not being dependent on the design on vb. so far i have.
Set rs...
|
by: gagonm |
last post by:
Hi
I m looking for some sample example (Code) for my following scenario
I have a Grid View which populates data from database say from Product Tables
using dataset and dataadapter
This...
|
by: vikas1111 |
last post by:
This is the code which i have written.....
Option Explicit
Dim c As New ADODB.Connection
Dim cm As ADODB.Command
Dim rs As New ADODB.Recordset
Private Sub regno_Change()
c.Open...
|
by: agarwalsunitadhn |
last post by:
I am creating gridview using connectionstring. i am passing different queries to bind grid as per condition. Now the problem I am facing on editing the grid. I am not able to use boundfield or...
|
by: shankarnag |
last post by:
i had dumped mysql database by using mysqldump.exe.If i didnt leave space between tables it is showing mysqldump.exe has stopped working.How can i overcome this problem?
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |