disable button in gridview - asp.net

I have a Gridview:
<asp:GridView ID="gvtransaction" runat="server" AutoGenerateColumns="False" Width="60%" OnRowDataBound="gvtransaction_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Bind("id") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Consumer">
<ItemTemplate>
<asp:Label ID="lblfirstname" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbllastname" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:Label ID="lblamount" runat="server" Text='<%# Bind("Amount") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Label ID="lblcurrencyID" runat="server" Text='<%# Bind("CurrencyID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Account Name">
<ItemTemplate>
<asp:Label ID="lblcurrencyname" runat="server" Text='<%# Bind("CurrencyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblstatus" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DateCreated">
<ItemTemplate>
<asp:Label ID="lbldatecreated" runat="server" Text='<%# Bind("DateCreated") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick="btnApprove_Click"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="btnReject" runat="server" Text="Reject" OnClick="btnReject_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
here is the code for the onclick = "btnApprove_click():
GridViewRow row = ((Button)sender).Parent.Parent as GridViewRow;
string id = ((Label)row.FindControl("lblid")).Text;
Response.Write(row.RowIndex);
string ApprovedStatus = "Approved";
Button btnApprove = (Button)sender;
btnApprove.Enabled = false;
string status = ClassBiller.ConsumerAcceptedStatus(int.Parse(id), ApprovedStatus, DateTime.Now);
ViewPendingConsumer(); //rebind gridview para magEffect yun update
my concern is, how can i disable the buttons inside my gridview when i clicked either the Approve Button or the Reject Button.
sample scenario:
when I click Approve, the buttons should be disabled so that the it will prevent the user to click the button again..
I have read some articles which suggests the use of gridview's onrowdatabound..But i am confuse on how to do it...
I tried using
row.Enabled = false;
still doesnt work...
help please..
thank you

You can try to disable the button inside its click event. But when your gridview databinds again, the button will be enabled back..
protected void btnApprove_Click(object sender, EventArgs e)
{
Button btnApprove = (Button)sender;
btnApprove.Enabled = false;
}

Try to use rowdatabound event of Grid View, Firstly Find the control by using FindControl method like
protected void gvtransaction_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// For `Approve` button
Button btnapprove = (Button)e.Row.FindControl("btnApprove"); // give property id of button form template field
btnapprove.Enabled = true; //true means enable else you may set false to disable button
// For `Reject` button
// Same condition but in `FindControl` method use `btnReject` id.
}
}
That's for Approve button For Reject button you may use same logic.
Link For Help
Hope it clear and works for you.

This is all you need to do, where Cells[5] is the cell where you have your Button, had the same problem and it worked for me.
protected void gvtransaction_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[5].Enabled = false;
}
}

Related

Setting EditIndex on a asp:GridView, can I be sure it is the correct row after rebinding to live data has been done?

Having an asp:GridView, when I put it into edit mode by setting EditIndex, I also need to update its data using the DataSource property and call DataBind like this:
protected void GrdFoo_OnRowEditing(object sender, GridViewEditEventArgs e)
{
grdFoo.EditIndex = e.NewEditIndex;
grdFoo.DataSource = DataBase.GetFoos();
grdFoo.DataBind();
}
Now, if the database contents was changed since I was populating the gridview first, how can I be sure I will edit the correct row?
edit
I got the suggestion to use OnRowDataBound. I am using a command field to trigger GrdFoo_OnRowEditing. Is the use of OnRowDataBound still a good idea?
Here is how I use the CommandField (aspx):
<asp:GridView ID="grdFoo" runat="server"
OnRowEditing="GrdFoo_OnRowEditing"
OnRowCancelingEdit="GrdFoo_OnRowCancelingEdit"
OnRowUpdating="GrdFoo_OnRowUpdating"
OnRowDeleting="GrdFoo_OnRowDeleting"
DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label Text='<%#Eval("Name") %>' runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" Text='<%#Eval("Name") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label Text='<%#Eval("Email") %>' runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmail" Text='<%#Eval("Email") %>' runat="server"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True"
ShowDeleteButton="True" buttontype="Image"
DeleteImageUrl="delete.png"
EditImageUrl="edit.png"
CancelImageUrl="cancel.png"
UpdateImageUrl="save.png"/>
</Columns>
</asp:GridView>

Row Edit event handler issue

<asp:GridView ID="GridView1" runat="server"
style="height: 121px; width: 175px" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
onrowediting="GridView1_RowEditing" AutoGenerateColumns="false">
<Columns>
<asp:CommandField ButtonType="Button" SelectText="Edit" ShowEditButton="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Id") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCpuName" runat="server" Text='<%# Eval("cpuname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("cpuname") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%# Eval("status") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("status") %>'>></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
GridViewRow row = GridView1.Rows[e.NewEditIndex];
TextBox txt = (TextBox)row.FindControl("TextBox2");
SqlDataSource1.UpdateParameters["CpuName"].DefaultValue = txt.Text;
SqlDataSource1.UpdateParameters["cpuid"].DefaultValue = "3";
SqlDataSource1.Update();
BindGridView();
}
I am getting a object reference not set to an object on the textbox txt. I am not able to get the textbox2 in my gridview.
Can you try this
GridViewRow row = GridView1.Rows[GridView1.NewEditIndex];
TextBox txt = row.Cells[1].Controls[1] as TextBox;
If (txt!= null)
{
SqlDataSource1.UpdateParameters["CpuName"].DefaultValue = txt.Text;
SqlDataSource1.UpdateParameters["cpuid"].DefaultValue = "3";
SqlDataSource1.Update();
BindGridView();
}
Why you just don't get directly the textbox value? is this a great problem?
...
GridViewRow row = GridView1.Rows[e.NewEditIndex];
SqlDataSource1.UpdateParameters["CpuName"].DefaultValue = row.Columns["CpuName"].Value;
...
I don't know that this works, because I'm a C#-er, and basically work on winform not on web apps
First, here is the snippet from MSDN, RowEditing topic:
The RowEditing event is raised when a
row's Edit button is clicked, but
before the GridView control enters
edit mode. This enables you to provide
an event-handling method that performs
a custom routine, such as canceling
the edit operation, whenever this
event occurs.
To resolve the problem, handle the RowUpdating event for this purpose. There is an example which should be helpful to you at:
GridView.RowEditing event
Also, I believe you should use the Bind word to bind your TextBox to a field:
'>
For more details, please refer to the Data-Binding Expressions Overview

Accessing Details View Bind Data Before The View Is Rendered

I have a field in a details view shown below
<asp:BoundField DataField="DTMON_F" HeaderText="Monday Hours From: " InsertVisible="False"
ReadOnly="True" SortExpression="HOURS" Visible="false" />
<asp:TemplateField HeaderText="Monday Hours From: " SortExpression="HOURS">
<EditItemTemplate>
<uc1:TimePicker ID="tpMondayHours" runat="server"/>
</EditItemTemplate>
<InsertItemTemplate>
<%-- <uc1:TimePicker runat="server" ID="tpMondayHours" />--%>
<asp:TextBox ID="txtMondayHours" runat="server" Text='<%# Bind("DTMON_F") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblMondayHours" runat="server" Text='<%# Bind("DTMON_F") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Before "DTMON_F" is binded to the view I want to cut the string that is returned...Where and how might I do this?
You can implement the OnDataBinding event for each control instead of doing the bind inline. This will give you the ability to do whatever you like with the data before assigning it to the control.
Example using your Label. The same could be applied to the TextBox:
<asp:Label ID="lblMondayHours" runat="server"
OnDataBinding="lblMondayHours_DataBinding"></asp:Label>
protected void lblMondayHours_DataBinding(object sender, System.EventArgs e)
{
Label lbl = (Label)(sender);
string yourValue = (int)(Eval("DTMON_F"));
// *** Do whatever you want with the value now
lbl.Text = yourValue;
}

Update textbox via onchange event of a dropdownlist while editing in gridview

I am trying to update a TextBox within currently edited row within a GridView on when changing items on a DropDownList and I cant quite get it going in VB. I found this code in c# but don't know if I'm on the right track?
Can you please offer some help?
PS: This code is for an OnMouseOver event but the point is to update the TextBox while in edit mode.
<ItemTemplate>
<asp:TextBox runat="server" ID="tx1" onmouseover='<%# "ChangeValue(" +((DataGridItem)Container).FindControl("tx1").ClientID + ")"%>'></asp:TextBox>
</ItemTemplate>
JS Code:
function ChangeValue(i)
{
var t=i.id
document.getElementById(t).value="Hello World!";
}
Try the following:
Put the following script in head tag:
<script language="javascript" type="text/javascript">
function ChangeValue(ddl,txtid)
{
var txt=document.getElementById(txtid);
var dval= ddl.options[ddl.selectedIndex].value;
if(dval=="1")
{
txt.value="It's 1";
}
if(dval=="2")
{
txt.value="It's 2";
}
if(dval=="3")
{
txt.value="It's 3";
}
}
</script>
Try the gridview below:
<asp:GridView ID="GridView1" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
AutoGenerateColumns="false" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand"
runat="server" OnRowCreated="GridView1_RowCreated">
<Columns>
<asp:CommandField ButtonType="link" ShowEditButton="true" ShowCancelButton="true" />
<asp:TemplateField HeaderText="CategoryID">
<ItemTemplate>
<asp:LinkButton ID="lnkID" runat="server" CommandName="sel" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.CategoryID") %>'
Text='<%# DataBinder.Eval(Container,"DataItem.CategoryID") %>'></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl" runat="server">
<asp:ListItem Text="1" Value="1" Selected="true"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container,"DataItem.CategoryID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtComments" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CategoryName">
<ItemTemplate>
<asp:LinkButton ID="lnkName" runat="server" CommandName="sel" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.CategoryName") %>'
Text='<%# DataBinder.Eval(Container,"DataItem.CategoryName") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Handle the rowcreated event in codebehind:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
TextBox txt = (TextBox)e.Row.FindControl("txtComments");
txt.Text = "It's 1";
//------------ Set onchange function for dropdown---------------------------//
ddl.Attributes.Add("onchange", "javascript:ChangeValue(this,'" + txt.ClientID + "');");
}
}
Bind the grid with Categories table in northwind db.

gridview control problem

work on C# asp.net vs05. i take a combo on gridview template field.
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3">
<Columns>
<asp:BoundField DataField="StudentID" HeaderText="StudentID" ReadOnly="True" SortExpression="StudentID" />
<asp:TemplateField HeaderText="DivisionName" SortExpression="DivisionName">
<ItemTemplate>
<asp:Label ID="lblDivisionName" runat="server" Text='<%# Bind("DivisionName") %>'
Width="116px"></asp:Label><br />
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StudentName" SortExpression="StudentName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("StudentName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("StudentName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" Text="Update" />
</Columns>
</asp:GridView>
Now i want to fill this combo by the bellow code ?
DropDownList1.DisplayMember = "CommercialRegionName";
foreach (object oItem in collection)
{
DropDownList1.Items.Add(oItem);
}
how to get the combo control id from the grid
You can use the following code:
Set OnRowDataBound of the gridview in aspx as OnRowDataBound="GridView3_RowDataBound"
Then put the following code in aspx.cs page.
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList DropDownList1 =(DropDownList) e.Row.FindControl("DropDownList1");
DropDownList1.DisplayMember = "CommercialRegionName";
foreach (object oItem in collection)
{
DropDownList1.Items.Add(oItem);
}
}
}

Resources