Disable LinkButton in DataGrid TemplateColumn - c#

I am using Visual Studio 2008 with .NET Framework 3.5. I have a DataGrid with a LinkButton inside a TemplateColumn. I am trying to figure out how to disable the ability to click the LinkButton once it has been clicked. My DataGrid has 6 columns with the LinkButton column displaying years and the others displaying year end data for those years. When a year is clicked the DataGrid displays a breakdown of that year's data on a month by month basis. When the DataGrid is displaying the month by month breakdown I still need the year column to be visible but without the ability to click. I also have a button and a chart that, by default Visibility is set to false, but after a year is selected the Visibility is set to true with the button giving the ability to close out of the month by month breakdown and return to the year end breakdown. I have everything working except the disabling of the LinkButton.
Here is the code for my DataGrid's TemplateColumn:
<asp:TemplateColumn HeaderText="Year End">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbYear" Text='<%# DataBinder.Eval(Container, "DataItem.year") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
I have tried the following:
Attempt 1 using code behind:
protected void Page_Load(object sender, EventArgs e)
{
LinkButton lb = ((LinkButton) FindControl("lbYear"));
lb.Attributes.Add("onClick", "return false;");
}
Attempt 2 using Javascript:
function disableLinkButton() {
var lb = document.getElementById("lbYear");
if (lb.disabled != true) { lb.disabled = true; return true; }
}
else { return false; }
}
<asp:TemplateColumn HeaderText="Year End">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbYear" OnClientClick="disableLinkButton()" Text='<%# DataBinder.Eval(Container, "DataItem.year") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
-- The 3rd attempt was close which did gray out the LinkButtons but did not disable the ability to click them
Attempt 3 using the 'Enabled' property:
<asp:TemplateColumn HeaderText="Year End">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbYear" Enabled='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem, "year"))==1?Convert.ToBoolean("True"):Convert.ToBoolean("False") %>' Text='<%# DataBinder.Eval(Container, "DataItem.year") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
Some other thoughts I have include using an 'OnClick' event or a 'CommandArgument'. I tried using 'OnClick' and in the code behind simply using:
LinkButton lb = ((LinkButton) FindControl("lbYear");
lb.Enabled = false;
Any help, thoughts, ideas, examples, etc. would be greatly appreciated. Thank you all in advance!
Adjusted Code:
<ItemTemplate>
<asp:LinkButton ID="lbYear" runat="server" OnClick="testClick" Text='<%# DataBinder.Eval(Container, "DataItem.year") %>'></asp:LinkButton>
<a id="MyContrl_lbYear" href="javascript:__doPostBack('MyContrl$lbYear','')" onclick="this.href='#';this.disabled=true;__doPostBack('MyContrl$lbYear','');"></a>
</ItemTemplate>
protected void showChart(object sender, EventArgs e)
{
LinkButton lbYear = ((LinkButton)FindControl("lbYear"));
lbYear.Attributes.Add("onclick", "this.href='#';this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(lbYear, "").ToString());
}

Option 3 and your last approach seem to be the way to go. The problem with LinkButtons is that even by putting Enabled on false, you wont block them from posting back. See: http://weblogs.asp.net/jeffwids/archive/2011/02/14/how-to-disable-an-asp-net-linkbutton-when-clicked.aspx
Therefore you have to do this manually with:
lb.Attributes.Add("onclick", "this.href='#';this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(lb, "").ToString());

Polity, thank you very much for your help, I really appreciate it. I found a different way to go about fixing this issue though.
.ASPX Code:
<TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" OnClick="test" Text='<%# DataBinder.Eval(Container, "DataItem.year") %></asp:LinkButton>
</ItemTemplate>
</TemplateColumn>
.ASPX.CS Code:
protected void test(object sender, EventArgs e)
{
foreach(var y in myDataGrid.Items)
{
LinkButton lb = ((y as TableRow).Cells[1].Controls[1] as LinkButton);
lb.Enabled = false;
}
}

Related

Error thrown when clicking image button in datalist

I have a datalist that displays images that I want to use as buttons, so I used the image button. But when I click the image, I get an error before it even gets to the if(e.CommandName == "imgButton") line. All I get is: 'System.Web.HttpUnhandledException' was thrown, I'm at a total loss. What am I doing wrong?
Thanks
<asp:DataList ID="dlImages" runat="server" RepeatDirection="Horizontal" RepeatColumns="3" OnItemCommand="dlImages_ItemCommand">
<ItemTemplate>
<asp:ImageButton ID="imgButton" runat="server" CommandName="imgButton"
CommandArugment='<%# Eval("id")%>'
ImageUrl='<%# Eval("url")%>' Style="max-width: 200px" />
</ItemTemplate>
</asp:DataList>
protected void dlImages_ItemCommand(object source, DataListCommandEventArgs e)
{
if(e.CommandName == "imgButton")
{
string taid = e.CommandArgument.ToString();
ShowModel(Toolkit.ToInt32(taid, 0));
}
}
All I needed to do was to not bind my datalist on postback. And currently, something is very wrong with this page. Like I need 3D glasses to read it.

how can I get the label where in the ItemTemplate with code behind C#?

I have a label where in the Itemtemplate ,I want to get the value of the label but
just can't find control with the label, please somebody help me? thank you
aspx code:
<asp:TemplateField HeaderText="工號" SortExpression="BS_ID">
<ItemTemplate>
<asp:Label ID="lblBSID" runat="server"
Text='<%# Eval("BS_ID") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Textbox ID="tbBS_ID_edit" runat="server" Text='<%# Eval("BS_ID") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="BS_ID_tb" Text="SK" MaxLength="7" runat="server" />
</FooterTemplate>
</asp:TemplateField>
code
This is what I var
//string lb_BS_ID = ((Label)GridView1.FindControl("lblBSID")).Text;
//this._userID = lb_BS_ID;
and I also try
//string lb_BS_ID = ((Label)GridView1.Rows[0].FindControl("lblBSID")).Text;
//this._userID = lb_BS_ID;
Also
//string lb_BS_ID =((Label)GridView1.Rows[e.RowIndex].Cells[0]FindControl("lblBSID")).Text;
//this._userID = lb_BS_ID;
but those are not work for me please help me><
instead of this:
string lb_BS_ID = ((Label)GridView1.FindControl("lblBSID")).Text;
Try this. :
Lable label = (Label)GridView1.Rows[e.RowIndex].FindControl("lblBSID");
string value = label.Text;
Note : If you just need to find the value of the label in first row then use Rows[0] and so on instead of e.RowIndex.
I hope it helps.
for updating the record use "OnRowUpdating" event of gridview;
while using your code 1,2 and 3 make sure that your grid is binded to some data.
you can not access any cntrol until your grid is binded to some data
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int trackingno = (int)GridView1.DataKeys[e.RowIndex].Value;
Label trackingno = (Label)GridView1.Rows[e.RowIndex].FindControl("yourLabelControlID");
//perform your update function
}
for other operations you can use "OnRowDataBound"
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddeditpickuprider = (DropDownList)e.Row.FindControl("yourDropdownControlID");
}

Finding drop down list control from a stored procedure in edit item template of gridview in asp.net c#

I am trying to find a DropDownList control on the EditItemTemplate of a grid view, to populate it with results from a query before it is drawn, but the control is never found.
ddlParent == null
Always!
I may be missing something very obvious but i have tried about 8 different methods to get this find control working, but no matter what i do, it comes up null.
I have included both the ASP and the C#, the sql should not be important as i cant even reach the call!
ASP:
<asp:TemplateField SortExpression="LocationArea2.Name" HeaderText="Parent Location Area">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("LocationArea2.Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlParent" runat="server" AppendDataBoundItems="true" DataTextField="LocationArea2.Name"
DataValueField="ParentID" AutoPostBack="false" SelectedValue='<%# Bind("ParentID") %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
C#:
protected void gvLocationArea_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (gvLocationArea.EditIndex == e.Row.RowIndex)
{
DropDownList ddlParent = (DropDownList)gvLocationArea.Rows[gvLocationArea.EditIndex].FindControl("ddlParent");
if (ddlParent != null)
{
using (SalesSQLEntities db = new SalesSQLEntities())
{
ddlParent.DataSource = db.GetRecursiveAreaList(Convert.ToInt32(((TextBox)gvLocationArea.Rows[gvLocationArea.EditIndex].FindControl("txtLocationAreaID")).Text), true);
ddlParent.DataBind();
ddlParent.Items.Add(new ListItem("* None", ""));
}
}
}
}
I know there is something missing here, the control is just never found no matter what i've tried!
Instead of doing a FindControl, use an offset index of the column in question and get the first control:
(DropDownList)gvLocationArea.Rows[gvLocationArea.EditIndex].Cells[INDEX OF THE DDL].Controls[0]

Accesing a CheckBox that's inside a Repeater

In my repeater's ItemTemplate I've a CheckBox and a disabled TextBox, I need to implement this idea: TextBox only gets enabled if the the CheckBox is checked .. so I set the CheckBox AutoPostBack to true and I tried to put this code in ItemDataBound. but I can't find my control which is weird because I use the same code but in loop "MyRptr.Item[i].FindControl...." and it works! .. I don't want to loop through all the Items, I just wish If I can know the Item number or location in which the CheckBox was created. and I've also tried to create an event handles for the CheckBox's CheckedChanged event but I can't find the CheckBox either!
protected void MyRptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
CheckBox ChkBx = e.Item.FindControl("IsSelected_ChkBx") as CheckBox;
if (ChkBx.Checked == true)
{
TextBox TxtBx = e.Item.FindControl("Value_TxtBx") as TextBox;
TxtBx.Enabled = true;
}
}
<asp:Repeater ID="MyRptr" runat="server"
onitemdatabound="MyRptr_ItemDataBound">
<ItemTemplate>
<asp:CheckBox ID="IsSelected_ChkBx" runat="server" Text='<%# Eval("Item") %>' AutoPostBack="True" OnCheckedChanged="IsSelected_ChkBx_CheckedChanged" />
<asp:TextBox ID="Value_TxtBx" runat="server" Enabled="false"></asp:TextBox>
<asp:HiddenField ID="ID_HdnFld" runat="server" Value='<%# Eval("ID") %>' />
</ItemTemplate>
<SeparatorTemplate>
<br></br>
</SeparatorTemplate>
</asp:Repeater>
So basically I need a clean and simple way to implement my logic and If I could get an explanation for what's happening it would be great, so any ideas =) ?
You can find your textbox as follow, but I think its better use the jQuery instead of server-side event
protected void IsSelected_ChkBx_CheckedChanged(object sender, EventArgs e)
{
var ch = (CheckBox)sender;
var txt = ch.Parent.FindControl("Value_TxtBx") as TextBox;
}

Need help with making label of repeater visible and link button hidden in code behind

aspx page:-
<asp:Repeater ID="rptAdd" OnItemCommand="rptAdd_ItemCommand" runat="server">
<ItemTemplate>
<td>
<asp:LinkButton ID="lnkBill" Text="Make Default" runat="server" Visible="true" CommandName="DefaultBill"></asp:LinkButton>
<asp:Label ID="labelBill" Text="Yes" Visible="false" runat="server"></asp:Label>
</td>
</ItemTemplate>
</asp:Repeater>
Code Behind:-
protected void rptAdd_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "DefaultBill")
{
Users objBill = new Users();
objBill.IsDefault = true;
e.Item.FindControl("labelBill").Visible=true;
e.Item.FindControl("lnkBill").Visible = false;
}
}
In code behind intellisense is not detecting "labelBill" and "lnkBill"..what could be wrong ?
Also need to know...that's how u access controls in a repeater ?? like using findControl() ...right ?
[EDIT]
Changed code as follows..still not working...
((Label)e.Item.FindControl("labelBill")).Visible=true;
((LinkButton)e.Item.FindControl("lnkBill")).Visible = false;
Why wont intellisense detect these two IDs??
The problem is that your controls are inside a repeater, the find control wont search recursively. Try this instead.
rptAdd.FindControl("labelBull").Visible = true;

Categories

Resources