i am new in asp.net i using LINQ with asp.net on button click event my gridview not rebind data and yes gridview is into the updatepanel
'>
'>
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvClientData.Rows)
{
if (((CheckBox)gvr.FindControl("chkdisplay")).Checked == true)
{
string Index = ((Label)gvr.FindControl("lblIndex")).Text;
int GIIndex = Convert.ToInt32(Index);
GI_InsureMaster insertclientinfo = vjdb.GI_InsureMasters.Single(upd => upd.GIMastIndex == GIIndex);
insertclientinfo.SendToCompany = true;
vjdb.SubmitChanges();
}
}
BindAgencyData();
Response.Redirect(Request.RawUrl);
}
It seems you are trying to modify an object and then saving it back to the DB, but you are doing it wrong.
You are querying the object from a different Data Context, vjdb and you are calling SubmitChanges on linqobject. You should call SubmitChanges on vjdb
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvClientData.Rows)
{
if (((CheckBox)gvr.FindControl("chkdisplay")).Checked == true)
{
string Index = ((Label)gvr.FindControl("lblIndex")).Text;
int GIIndex = Convert.ToInt32(Index);
GI_InsureMaster insertclientinfo = vjdb.GI_InsureMasters.Single(upd => upd.GIMastIndex == GIIndex);
insertclientinfo.SendToCompany = true;
vjdb.SubmitChanges(); //HERE
}
}
BindAgencyData();
Response.Redirect(Request.RawUrl);
}
Assuming that BindAgencyData is querying database for latest/updated record and then binding the data to the grid.
Related
I am trying to save my GridView data to a database using a foreach loop.
but the program is not entering the loop.
protected void btnAdd_Click(object sender, EventArgs e) {
foreach (GridViewRow row in grdYarnIssue.Rows) {
tbl_YarnIssueTemp item2 = new tbl_YarnIssueTemp();
item2.LotNo = Convert.ToInt32(row.Cells[2].Text.Trim());
item2.Unit = Convert.ToString(row.Cells[3].Text);
item2.IssueQuantity = Convert.ToInt32(row.Cells[4].Text);
item2.Rate = Convert.ToInt32(row.Cells[].Text);
dbcontext.tbl_YarnIssueTemp.AddObject(item2);
dbcontext.SaveChanges();
}
}
I have a Datalist that has a delete button in it , I want to check if the user is admin, shows this button for each item and if not dont show that. This is my ItemDataBound, my problem is that for each item it connects to database and check if user is admin or not and I am worried about getting slow for large number of items. How can I fix this problem?
protected void GroupMembersDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton deletButton = (LinkButton)e.Item.FindControl("DeletFromGroup");
// here first connecet to database to get group name
int GroupID = BusinessLayer.Group_Table.GetByUniqName(Page.RouteData.Values["GroupName"].ToString());
// here connect to database and find groups admin ID and checks that are the same or not ?
BusinessLayer.Group_Table GObject = new BusinessLayer.Group_Table(GroupID);
if (Convert.ToInt32(Session["ID"].ToString()) == GObject.Id)
{
deletButton.Visible = true;
}
else
{
deletButton.Visible = false;
}
}
}
Edit solution:
private BusinessLayer.Group_Table GObject;
private int GroupID;
protected void Page_Load(object sender, EventArgs e)
{
try
{
int ID = Convert.ToInt32(Session["ID"].ToString());
GroupID = BusinessLayer.Group_Table.GetByUniqName(Page.RouteData.Values["GroupName"].ToString());
GObject = new BusinessLayer.Group_Table(GroupID);
}
catch( Exception ee )
{
Response.Redirect("~/Home.aspx");
}
if (!IsPostBack)
{
getdata();
}
}
You could initialize a field in the class in Page_Load or the DataList's DataBinding event. Then you can access this field from ÌtemDataBound.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int GroupID = BusinessLayer.Group_Table.GetByUniqName(Page.RouteData.Values["GroupName"].ToString());
GObject = new BusinessLayer.Group_Table(GroupID);
DataBindDataList(); // code that calls DataBind
}
}
private BusinessLayer.Group_Table GObject;
Another way is to store this value in the Session.
I followed this tutorial on MSDN for ASP.NET GridView Update Row, but it does not work.
updatedItem.DepartureCity = ((TextBox)(row.Cells[2].Controls[0])).Text;
Still gives the original value from the cell and not the updated one.
public partial class ManagePage : System.Web.UI.Page
{
BusScheduleModelContainer modelContainer = new BusScheduleModelContainer();
protected void Page_Load(object sender, EventArgs e)
{
//FormsAuthentication.RedirectFromLoginPage()
//if (!HttpContext.Current.User.Identity.IsAuthenticated)
//{
// Server.Transfer("LoginPage.aspx");
//}
resultsGridView.DataSource = modelContainer.BusRoutes.ToList();
resultsGridView.DataBind();
}
protected void RowDeleting(object sender, GridViewDeleteEventArgs e)
{
var routeID = int.Parse(e.Values[0].ToString());
var removedItem = modelContainer.BusRoutes.FirstOrDefault(
item => item.RouteID == routeID);
if (removedItem != null)
{
modelContainer.BusRoutes.Remove(removedItem);
resultsGridView.DataSource = modelContainer.BusRoutes.ToList();
resultsGridView.DataBind();
modelContainer.SaveChanges();
}
}
protected void RowUpdating(object sender, GridViewUpdateEventArgs e)
{
var routeID = int.Parse(e.NewValues[0].ToString());
var updatedItem = modelContainer.BusRoutes.FirstOrDefault(
item => item.RouteID == routeID);
if (updatedItem != null)
{
GridViewRow row = resultsGridView.Rows[e.RowIndex];
var res = row.FindControl("ctl00$ContentPlaceHolder1$resultsGridView$ctl02$ctl03");
updatedItem.DepartureCity = ((TextBox)(row.Cells[2].Controls[0])).Text;
updatedItem.ArrivalCity = ((TextBox)(row.Cells[3].Controls[0])).Text;
updatedItem.DepartureTime = DateTime.Parse(((TextBox)(row.Cells[4].Controls[0])).Text);
updatedItem.ArrivalTime = DateTime.Parse(((TextBox)(row.Cells[5].Controls[0])).Text);
}
resultsGridView.EditIndex = -1;
BindData();
}
protected void RowEditing(object sender, GridViewEditEventArgs e)
{
//Set the edit index.
resultsGridView.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindData();
}
protected void RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//Reset the edit index.
resultsGridView.EditIndex = -1;
//Bind data to the GridView control.
BindData();
}
private void BindData()
{
resultsGridView.DataSource = modelContainer.BusRoutes.ToList();
resultsGridView.DataBind();
}
}
<div>
<asp:GridView runat="server" ID="resultsGridView"
AutoGenerateColumns="true" AllowPaging="true"
AutoGenerateDeleteButton="true" OnRowDeleting="RowDeleting"
AutoGenerateEditButton="true" OnRowUpdating="RowUpdating"
OnRowEditing="RowEditing" OnRowCancelingEdit="RowCancelingEdit">
</asp:GridView>
</div>
Do you use CommandField for update controler?
If so, when you click update button, first it will do Page_Load event handler, after that do the implementation in RowUpdating event handler.
You should try to check post back in Page_Load event handler like this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
resultsGridView.DataSource = modelContainer.BusRoutes.ToList();
resultsGridView.DataBind();
}
}
By this way, it will bind data to the GridView only first time you open this page.
For post back event such as clicking update button, it will not bind the original data to GridView again.
In RowUpdating method you need to add modelContainer.SaveChanges(); like below:
if (updatedItem != null)
{
GridViewRow row = resultsGridView.Rows[e.RowIndex];
var res = row.FindControl("ctl00$ContentPlaceHolder1$resultsGridView$ctl02$ctl03");
updatedItem.DepartureCity = ((TextBox)(row.Cells[2].Controls[0])).Text;
updatedItem.ArrivalCity = ((TextBox)(row.Cells[3].Controls[0])).Text;
updatedItem.DepartureTime = DateTime.Parse(((TextBox)(row.Cells[4].Controls[0])).Text);
updatedItem.ArrivalTime = DateTime.Parse(((TextBox)(row.Cells[5].Controls[0])).Text);
modelContainer.SaveChanges();
}
I have some data in ObjectDataSource, before binding the data to the GridView, I want to remove some rows from the DataSource.
This is what I am trying:
protected void gvExitInterview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
User employee = (User) e.Row.DataItem;
if(//some condition here)
{
//do nothing
}
else
{
//delete the row
this.gv.DeleteRow(e.Row.RowIndex);
return;
}
}
}
These are my deletion methods:
protected void gvExitInterview_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void gvExitInterview_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
gv.DataBind();
}
This is my grid binding:
private void BuildGrid(DateTime from, DateTime to)
{
this.objDS.TypeName = "EmployeeManagement";
this.objDS.SelectMethod = "GetEmployees";
this.objDS.SelectCountMethod = "GetEmployeesCount";
this.objDS.SelectParameters.Clear();
this.objDS.SelectParameters.Add("from", from.ToString());
this.objDS.SelectParameters.Add("to", to.ToString());
this.objDS.SelectParameters.Add("csvEntities", csv);
this.objDS.SelectParameters.Add("sortExpression", ViewState["SortColumn"].ToString());
this.gv.DataSource = objDS;
this.gv.DataBind();
}
This is not working, it does not filter or delete any data from the grid. Any idea how to do explicit deletion?
I think you need to write another SelectMethod in the DataObjectClass which get a parameter which you use pass to filter condition. So that just returning with the rows required to display.
i have a gridview with tempaltefield buttons,
i want to create a session with value of a cell in selected button row ,
can anyone help me i tryed this but didnt work:
protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
{
Session["mysession"] = GridView1.SelectedRow.Cells[1].Text;
}
First of all, if it's just a imagebutton in a templatefield, actually you don't select de row. This line will problably throw an exception because SelectedRow is null.
But if you are using a command to select, that's correct. Maybe your event (ImageButton1_Click1) is not assigned to your image (OnClick).
You can try something like this:
protected void Page_Load(object sender, EventArgs e)
{
try
{
//Add and event RowDataBound
grvGrid.RowDataBound += new GridViewRowEventHandler(grvGrid_RowDataBound);
}
catch
{
//...throw
}
}
protected void grvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.Header)
{
//...
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Add an ImageButton foreach row in GridView
ImageButton ibtImageAlt = new ImageButton();
ibtImageAlt.ImageUrl = "App_Images/y.gif";
//ImageButton's ID will be the index of the row
ibtImageAlt.ID = e.Row.RowIndex.ToString();
ibtImageAlt.ForeColor = System.Drawing.Color.White;
ibtImageAlt.Font.Overline = false;
ibtImageAlt.Click += ibtImageAlt_Click;
}
}
catch
{
//...throw
}
}
protected void ibtImageAlt_Click(object sender, EventArgs e)
{
try
{
//Catch the ImageButton ID and the row in GridView
//An example to catch the value of the row selected by the ImageButton
Int32 intIndexRow = Convert.ToInt32(((ImageButton)sender).ID);
String strTest = grvGrid.Rows[intIndexRow].Cells[0].Text;
}
catch
{
//...throw
}
}