I have two LinkButton's called Approve and Reject in my GridView and I want my Approve LinkButton to be Disabled when click on it. I tried the following code but it's not working.
protected void gvManagerTimeSheet_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ApproveRow")
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
LinkButton lnkbtn = (LinkButton)row.FindControl("lbApprove");
lnkbtn.Enabled = false;
int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
int TimeSheetId = Convert.ToInt32(e.CommandArgument);
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("spApproveTimeSheet ", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#TimeSheetId", TimeSheetId);
con.Open();
cmd.ExecuteNonQuery();
GetManagerTimeSheets();
}
}
}
Well you haven't shown your aspx link button so i assuming that your link button is this
<asp:LinkButton id="linkBtn" runat="server" text="Approve" OnClientClick="Disable(this);"/>
Now you should add a javascript function like this on the page::
<script>
function Disable(link)
{
link.disabled = result;
}
</script>
Now when you click on the page your button will get disabled.
try this
LinkButton lbApprove = (LinkButton)e.Row.Cells[0].Controls[0];
You need to Rebind the grid with disabled control, but also you need to check the status in itembound event and disable. For that you can use session or hidden field.
protected void rg_OnItemCommand(object source, GridCommandEventArgs e)
{
// your logic
hdFlag.value = "val" // id of the data item to hide if more than one use array
// rebind logic for gird
}
protected void rg_ItemDataBound(object sender, GridItemEventArgs e)
{
if(hdFlag.value == "id")
{
// Find the control and hide
}
}
Related
I have a gridview with checkbox controls as columns and with Autopostback='true' for inserting to db.
Everyting works fine except when i "change pages" - Postback (?)
My checked checkboxes gets unchecked when I go back to the page.
My Page_Load have a !IsPostBack and then i bound my gridview (gwPlanning)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridViewPlanning();
}
}
and my code behind:
protected void myCheckBox_OnCheckedChange(object sender, EventArgs e)
{
CheckBox myCheckBox = (CheckBox)sender;
GridViewRow row = (GridViewRow)myCheckBox.NamingContainer;
string ThisWeekMinus2 = row.Cells[1].Text;
bool status = myCheckBox.Checked;
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("UPDATE [BI_Planning].[dbo].[tblPlanning] SET [This Week - 2] = #IsActive WHERE ActivityID = #ID ", con);
cmd.Parameters.Add("#IsActive", SqlDbType.Bit).Value = myCheckBox.Checked;
cmd.Parameters.Add("#ID", SqlDbType.Int).Value = ThisWeekMinus2;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
any idea what i'm missing?
The solution was just to add:
Checked='<%#Convert.ToBoolean(Eval("....YourColumn...."))%>
<asp:CheckBox ID="CheckBox1" AutoPostBack="true" Checked='<%#Convert.ToBoolean(Eval("....YourColumn...."))%> OnCheckedChanged="myCheckBox_OnCheckedChange" runat="server" Style="text-align: center" />
I have a header DDL in my gridview that for some reason does not keep my selected value but rather binds the gridview and the header back to "starting position"
In my DDL header for Priority i have selected value '99' but after that my header gets back to starting position of my ListItem (that is Priority)
<HeaderTemplate>
<asp:DropDownList ID="ddlPriorityHeader" AutoPostBack="True" AppendDataBoundItems="True" OnSelectedIndexChanged="ddlHeader_SelectedIndexChanged" runat="server">
<asp:ListItem>Priority</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
I have a RowDatabound for the gridview but there i do nothing more then find the DDL for the header and then bound the DDL.
protected void gwActivity_RowDataBound(object sender, GridViewRowEventArgs e)
{
//.............. some code.....//
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("SELECT DISTINCT [Priority] FROM [BI_Planning].[dbo].[tblPriority]", con);
con.Open();
ddlPriority.DataSource = cmd.ExecuteReader();
ddlPriority.DataTextField = "Priority";
ddlPriority.DataBind();
}
}
I have put my gridview in a method:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewActivity();
}
}
Can it be that im bounding my DDL everytime for my gridview? im stuck here....
You need to store ddlPriority selected text in temp place like ViewState that will keep value between postbacks.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["PriorityText"] = "Priority";
BindGridviewActivity();
}
}
And aftrt ddlPriority.DataBind(); set selectd text to ViewState value
ddlPriority.Items.FindByText(ViewState["PriorityText"].ToString()).Selected = true;
And in your ddlHeader_SelectedIndexChanged set ViewState to selected text
protected void ddlHeader_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlHeader = (DropDownList)sender;
ViewState["PriorityText"] = ddlHeader.SelectedItem.Text;
}
Check this complete example for more details
I have DetailsView with connected SqlDataSource and I want cancel updating row when my condition is true in OnRowUpdating event
I did this step but when condition is true DetailsView still at editing mode all I need to return to normal view if the condition is true
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
// Iterates through the rows of the GridView control
foreach (DetailsViewRow row in DetailsView1.Rows)
{
// Selects the text from the TextBox
// which is inside the GridView control
// Selects the text from the DropDownList
// which is inside the GridView control
string dropDownListText = ((DropDownList)
row.FindControl("DropDownList12")).SelectedItem.Text;
SqlConnection conn = new SqlConnection(GetConnectionString());
SqlCommand cmd2 = new SqlCommand();
conn.Open();
cmd2.CommandText = #"Select StatusID from ComplainMain Where TicketID=#tktid";
cmd2.Parameters.AddWithValue("#tktid", ticketid.tktid);
cmd2.Connection = conn;
SqlDataReader rdr = cmd2.ExecuteReader();
int status;
while (rdr.Read())
{
status = rdr.GetInt32(0);
if (status == 3)
{
Label18.Visible = true;
Label18.Text = "Can not Modify this Complaint as Ticket Closed refer to the Admin";
e.Cancel = true;
}
}
}
}
Page_Load
protected void Page_Load(object sender, EventArgs e)
{
Label18.Visible = false;
DetailsView1.ItemUpdating += new DetailsViewUpdateEventHandler(DetailsView1_ItemUpdating);
}
Use ChangeMode method on your DetailsView
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
I asked a similar question to this but the circumstances have changed.
I bind my gridview through code rather than on the source.
The pagination works fine, but if I click a button on second page of the gridview (after pagination), the postback is causing the pagination to reset to page 1. Can anyone tell me what I'm doing wrong?
Within my pageload i have set the !POSTBACK method as shown i.e if there is postback event, then it shouldn't reset the grid but it does!
Heres the onload:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["usersName"] != null)
{
object a = Session["_id"];
IDMaster = Convert.ToInt32(a);
GridView1.Columns[10].Visible = true;
GridView1.Columns[11].Visible = true;
}
else
{
GridView1.Columns[10].Visible = false;
GridView1.Columns[11].Visible = false;
}
if (!IsPostBack)
{
BindGrid();
}
The BindGrid();
SqlConnection sqlcon = new SqlConnection(connstring);
SqlCommand sqlcmd = new SqlCommand("select * from Coffees ORDER BY coffeeName ASC", sqlcon);
SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
Page index method:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
if(ViewState["searchTerm"] != null)
{
object a = ViewState["searchTerm"];
string reloadTerm = a.ToString();
setGrid(reloadTerm);
}
You need to bind your gridview in GridView1_PageIndexChanging event
GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
if(ViewState["searchTerm"] != null)
{
object a = ViewState["searchTerm"];
string reloadTerm = a.ToString();
setGrid(reloadTerm);
}
BindGrid();
}
hopefully it works for you.
Since you are binding grid view dynamically, Please remove
if (!IsPostBack)
condition from page load. Grid view needs binding every time.
I found this issue. I forgot that after I add an item to my cart i was calling response.redirect to refresh the page....obviously this meant the page was recalled refreshing the page so the grid was always going to reset. Thanks again.
i am getting the data from gridview on rowcommand event by the following code
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "editproject")
{
string rowindex = e.CommandArgument.ToString();
int index = int.Parse(rowindex);
GridViewRow row = GridView2.Rows[index];
Label6.Text = row.Cells[1].Text;
}
}
but it would give only the data of the field that is visible in gridview row .how can i get the field that is not visible but bound to the gridview.
You can't get the value that you set to invisible because these were not rendered at the client side and can't be grabbed on the server side.
Alternatively you can store the value in hidden field and then you can get it from hidden field.
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(dataGridView1.SelectedRows[0].Cells[0].Value.ToString ());
}
You can,t get the invisible bound elements but you can get the value from data source .For example you save data in data table which assigned to grid .Store this data table in view-state and on row command get data-key of that row and retrieved value through data table
You can get a command-like button which is invisible in the gridview, just look at this:---
False visibility of button requires you to have changed the property EnableEventValidation="False" on the page directive in default.aspx
private void grd_bind()
{
SqlDataAdapter adp = new SqlDataAdapter("select* from tbbook", ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lk = (LinkButton)(e.Row.Cells[5].Controls[0]);
e.Row.Attributes["Onclick"] = ClientScript.GetPostBackClientHyperlink(lk, "");
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
TextBox2.Text = GridView1.SelectedRow.Cells[1].Text;
TextBox3.Text = GridView1.SelectedRow.Cells[2].Text;
TextBox4.Text = GridView1.SelectedRow.Cells[3].Text;
TextBox5.Text = GridView1.SelectedRow.Cells[4].Text;
}
then in the default.aspx page, set EnableEventValidation
<%# Page Language="VB" EnableEventValidation="false" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>