Asp.net LinkButton, set visibility - c#

I have the following asp.net setup:
<tr runat="server" id="trDecline" class="even">
<td class="adminrow-name">Decline Applicant:</td>
<td colspan="2">
Click Here
<div class="divDecline" style="display:none;">
<asp:DropDownList runat="server" ID="ddDeclineReason" CssClass="ddDeclineReason" DataTextField="StatusDetail" DataValueField="ProposalStatusDetailID" OnSelectedIndexChanged="ddDeclineApplicant_Selected" AutoPostBack="true"/> <br>
<asp:LinkButton runat="server" visible="false" ID="btnApplicantDecline" Text="Decline" ValidationGroup="declineApplicantButton" onClick="btnDecline_Applicant_Click" />
</div>
</td>
</tr>
where I set the visibility of the LinkButton using visible="false"
Then, when someone selects an item from the dropdown, I want the LinkButton to become visible.
I have the following listener setup on the dropdown:
public void ddDeclineApplicant_Selected(object sender, EventArgs e)
{
Response.Write(btnApplicantDecline.visible);
Response.End();
}
The problem is, I get the following error:
System.Web.UI.WebControls.LinkButton' does not contain a definition for 'visible' and no extension method 'visible' accepting a first argument of type 'System.Web.UI.WebControls.LinkButton' could be found (are you missing a using directive or an assembly reference?)
But how can this be the case, as I am setting the property, andi t is working.
How can I set the visibility of this programatically if I cant use this control?
I know I can use CSS, but I want to keep within the existing framework of the project.
My google searches have not yielded any results.

Change "visible" for "Visible". V is UpperCase.

Try this.
You can see that Visible turn color to blue,because it have to be UpperCase
<tr runat="server" id="trDecline" class="even">
<td class="adminrow-name">Decline Applicant:</td>
<td colspan="2">
Click Here
<div class="divDecline" style="display:none;">
<asp:DropDownList runat="server" ID="ddDeclineReason" CssClass="ddDeclineReason" DataTextField="StatusDetail" DataValueField="ProposalStatusDetailID" OnSelectedIndexChanged="ddDeclineApplicant_Selected" AutoPostBack="true"/> <br>
<asp:LinkButton runat="server" Visible="false" ID="btnApplicantDecline" Text="Decline" ValidationGroup="declineApplicantButton" onClick="btnDecline_Applicant_Click" />
</div>
</td>
</tr>

Related

Attempting to hide <td> from C# causes error: The name '' does not exist in the current context

Goal: I am trying to (conditionally) hide a value depending on my settings
What I've tried:
I first tried to do it like I have in other places, but I was hiding asp controls like Panel. This is just html.
When looking this up, it was mentioned to add:
1. id
2. runat=server
Problem: I tried these, but I am getting the error:
"The name 'groupid' does not exist in the current context"
CODE:
.aspx:
<td id="groupid" runat="server">
.cs:
groupid.Visible = true;
is in an ItemTemplate for a asp:ListView:
<asp:ListView>
<LayoutTemplate>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
If the Control is in a ListView (or a Repeater, GridView etc.) you have to use FindControl. And because you use a "normal" td with runat=server you need to cast it to an HtmlTableCell.
HtmlTableCell htc = ListView1.Items[1].FindControl("groupid") as HtmlTableCell;
htc.Attributes.Add("style", "background-color: red");
//or
htc.Visible = false;
Although I would recommend against hiding individual table cells. This can give weird results in a browser.
td is table cell in HTML.
In ASP.NET you can use <asp:TableCell> which is equivalent to td.
Give the Id to <asp:TableCell> instead of using <td>
Does this solve your problem?
Updates:
C#:
protected void ListView1_DataBound(object sender, EventArgs e)
{
ListView1.FindControl("tdotherItem").Visible = false;
}
ASPX:
<asp:ListView ID="ListView1" runat="server" DataSourceID="MyDataSource" ItemPlaceholderID="itemPlaceHolder">
<LayoutTemplate>
<table>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td runat="server" id="myCol" visible='<%# (bool)Eval("otherItem") %>'>
<%# Eval("other") %>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Notice the use of visible ='<%# %>'

html input of type File wrapped inside ASP: Panel is not disabled when panel is disabled

I have wrapped HTML Input control of type File inside ASP:Panel control(which is wrapped inside update panel).
When I disable ASP:Panel control, input control is still enable. Please helpme out
ASPX Code :
<asp:Panel ID="pnlBrowseCSV" runat="server" Enabled="true">
<table>
<tr>
<td align="left" valign="top" style="height: 30px; width: 160px;">
<strong>CSV File:</strong>
</td>
<td style="height: 30px">
<input type="file" id="csvFile" runat="server"
onkeydown="return false" style="width: 350px; background-color:white"/>
<strong>(*.csv)</strong>
</td>
<td style="height: 30px">
<ASP:Button ID="btnValidate" Text="Validate" runat="server"
OnClick="btnValidate_Click" />
</td>
</tr>
</table>
</asp:Panel>
Using visible attribute instead of Enabled.
<asp:Panel ID="pnlBrowseCSV" runat="server" Visible="False">
This issue is by design. What you can do rather is write one extra line of code to disable File upload where you disable the panel.
pnlBrowseCSV.Enabled = false;
csvFile.Enabled=false;

Required Validator and compare validators do not work!

I am trying to see the validation part working. I have few required field validators and compare field validators, etc.
<div>
<asp:RequiredFieldValidator ID="rfvCompany" ValidationGroup="groupProfile"
ControlToValidate="txtCompany" runat="server"
ErrorMessage="- Company Name Required" Display="Dynamic" />
</div>
<div>
<asp:RequiredFieldValidator ID="rfvAddress" ValidationGroup="groupProfile"
ControlToValidate="txtAddress" runat="server"
ErrorMessage="- Address 1 Required" Display="Dynamic" />
</div>
This is my save button, validation has to happen when this button is clicked.
<tr>
<td align="center">
<asp:ImageButton ID="btnSave" runat="server" ImageUrl="~/images/green-save.gif"
OnClick="btnSave_Click" TabIndex="22" ValidationGroup="groupProfile" />
</td>
</tr>
The popup that comes when the save button is clicked is this..
<tr>
<td colspan="2" align="left" style="padding-left: 75px; padding-top: 10px;">
Do you wish to update the Location Information as well.
</td>
</tr>
<tr>
<td align="center" colspan="4">
<asp:Button ID="btnYesMerchant" Text ="Yes" runat="server" class="popupButton"
causesvalidation="true" OnClientClick="$find('mdlpop').hide(); return true;"
onclick="btnYessave_Click"/>
<asp:Button ID = "btnNoMerchant" Text ="No" runat ="server" class="popupButton"
causesvalidation="true" OnClientClick="$find('mdlpop').hide(); return true;"
onclick="btnNosave_Click"/>
<asp:Button Id="btnCancel" Text ="Cancel" runat="server" class="popupButton" />
</td>
</tr>
Where am i doing wrong? i am in a serious mess, i guess :(
Ensure you have included the asp:ScriptManager on your page. In addition to that check for javascript errors and (if any) add them to your question.
Here i have assigned the popup to the button, so the popup is being called when the button is clicked. Now i have changed the flow. For the button, i have written a javascript function that will validate and calls the popup to show if validation is passed. This worked. Thank you all..

Two forms in ASP.NET

You can only have one form runat="server" per page apparently.
My page has one form, where it loads in a list of names. This form allows you to add a new name to the list as well.
I've attatched an onclick event to each name in the listview. When you click on it, I need it to load the data into the edit form (next to the add form) with JavaScript code; I can do this fine.
But how do I structure it on the page to have two forms?
An illustration:
<table>
<tr>
<td style="width:50%;" valign="top">
<form runat="server" action="productCats.aspx?action=new&mid=2">
<div class="subHead">Create New Category</div>
<table class="settingTable">
<tr>
<td colspan="2"><b>Category Name</b></td>
</tr>
<tr>
<td>
<asp:TextBox ID="catName" runat="server" CssClass="tbox widebox"></asp:TextBox>
<asp:RequiredFieldValidator runat="server"
id="ValidatorName"
ControlToValidate="catName"
ErrorMessage="You need to enter a category name"
display="Dynamic" />
</td>
</tr>
<tr>
<td>This is the name of your category.</td>
</tr>
<tr>
<td colspan="2"><b>Parent Category</b></td>
</tr>
<tr>
<td>
<asp:ListBox SelectionMode="Single" Rows="8" id="parent" runat="server" CssClass="tbox widebox">
<asp:ListItem Selected="True" Text="Top Level" Value="0"></asp:ListItem>
</asp:ListBox>
<asp:RequiredFieldValidator runat="server"
id="RequiredFieldValidator1"
ControlToValidate="parent"
ErrorMessage="You need to select a parent"
display="Dynamic" />
</td>
</tr>
<tr>
<td>Choose a parent this category belongs to.</td>
</tr>
</table>
<asp:Button id="id" text="Create" runat="server" />
</form>
</td>
<td style="width:4%;">
</td>
<td valign="top">
<div class="subHead">Modify Category</div>
<form id="Form1" action="productCats.aspx?action=update&mid=2">
<table class="settingTable">
<tr>
<td colspan="2"><b>Category Name</b></td>
</tr>
<tr>
<td>
<asp:TextBox ID="newCatName" runat="server" Enabled="false" CssClass="tbox widebox"></asp:TextBox>
<asp:RequiredFieldValidator runat="server"
id="RequiredFieldValidator2"
ControlToValidate="newCatName"
ErrorMessage="Enter a new category name"
display="Dynamic" />
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
ASP.NET Web Forms works by having just one <form> element on a page, and getting this posted back to the same page every time something changes (postbacks). Trying to use multiple forms, and specifying custom action attributes on the form element is going against what the framework is designed to work with, and that's never really a good idea.
I would just try to get rid of the second <form> element, and remove the action attribute from the first <form>. Also, ASP.NET will be much happier if everything is inside the form, i.e. your <table> tags at the top of the page.
I'm not sure what your page is doing, but if you've got a TextBox and you're using the contents of this to add items to a ListBox, a more Web Forms-like approach would be to use some control to do a postback when the TextBox has been filled in, and then re-bind the ListBox to some kind of data source. Maybe use an UpdatePanel if you want an Ajax postback.
If you're more comfortable with JavaScript and query string parameters, maybe ASP.NET MVC would be a better fit.
By what I understood from your explanation, you want a functionality on this page of yours where when you click on one of the listitem in the list, the elements in the form2 needs to displayed and to modify the details... What I think is ... use two Panels and onclick of the item in ListBox you can show the Edit Panel and on the change of details and some other event like button click .. you can show the ListBox Panel with the changed detail.
Yes as Graham has mentioned you can use Ajax update panel to accomplish this.

why can't asp.net find my textbox?

I'm trying to add more fields to the CreateUserWizardStep, here is what I added:
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
<ContentTemplate>
<table border="0">
<tr>
<td align="right">
<asp:Label ID="NickNameLabel" runat="server" AssociatedControlID="NickName">Nick Name:</asp:Label>
</td>
<td>
<asp:TextBox ID="NickName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="NickName"
ErrorMessage="Nick Name is required." ToolTip="Nick Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
</td>
</tr>
<%-- The default code is left unchanged, but not shown here --%>
</table>
</ContentTemplate>
</asp:CreateUserWizardStep>
Then I tried to reference the objects like this
protected void NewUserWizard_CreatedUser(object sender, EventArgs e)
{
CreateUserWizardStep step = NewUserWizard.FindControl("CreateUserWizardStep1") as CreateUserWizardStep;
TextBox nickName = step.FindControl("NickName") as TextBox;
// insert additional information to the database
}
The problem is, I'm getting nulls for nickName. Am I using FindControl("") incorrectly?
you may want to use a recursive find control function such as here: http://stevesmithblog.com/blog/recursive-findcontrol/
FindControl only
Searches the current naming container for the specified server control.
i.e. It only checks the current containers direct children.
You can use the Controls property to return all the children of step:
ControlCollection children = step.Controls;
and enumerate over this looking for your text box.
I don't know the control myself, but doesn't CreateUserWizardStep1.NickNameLabel work?

Categories

Resources