I need to get a label during runtime so that i can compare to the Session["userName"] and choose to show buttons after the comparison.
I have look around and this seems to be the solution but i've run onto trouble as i only get null during runtime.
UserName:
<asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' />
<br />
So, this is the label inside a "ListView1" i need what Eval finds during each iteration of pumping out of the attached database.
<% Label lbl = (Label) ListView1.FindControl("userName");
string userName = lbl.Text;
if (Session["userName"].ToString() == userName)
{%>
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
<% }%>
and this is my attempt at retrieving the label.Text. i know it is something similar to this but i might be doing it wrong... well i am doing it wrong.
Thanks you very much for any information you can provide as i have just spent about 10 hours changing the ways i could achieve this result.
I would do something like:
Visible='<%# Session["userName"].ToString() == Eval("UserName").ToString() %>'
on each of the Button controls.
Related
I have a ASP Gridview that has a column with a button that opens a popover. Inside the popover I want to populate an ASP Table or ASP Gridview to show hidden related data. I am able to open the popover and it displays hard coded data. But when I want to make the data result dynamic using the Eval() tag the syntax is not able to read the text="<%# Eval('Label1') %>" tags within the string within the object.
The purpose is to load the entire data set and the popover data on one page load. I am wanting to avoid reload if possible. It seems that it cannot handle the <% within the string, is there a way to handle for this?
Any advice is welcome. Thank you in advance.
Sample code:
<asp:Button ID="Button3"
runat="server"
class="btn btn-info btn-xs"
Text="+"
OnClientClick="return false;"
data-toggle="popover"
data-trigger="focus"
TabIndex="0"
data-placement="right"
title="Owner Change"
UseSubmitBehavior="true"
data-content='<asp:Gridview runat="server" ID="gdvTest">
<Columns>
<asp:TemplateField HeaderText="Label One">
<asp:ItemTemplate>
<asp:Label runat="server" text="<%# Eval('Label1') %>" />
</asp:ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Label Two">
<asp:ItemTemplate>
<asp:Label runat="server" text="<%# Eval('Label2') %>" />
</asp:ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
/>
Update: I also attempted to use ASP Table, but my class is not recognizing the ID im trying to use to dynamically populate the Label.
...data-content="<asp:Table><asp:TableRow><asp:TableHeaderCell text='TestHeader'><asp:Label runat='server' ID='lblTest' style='color:Red'></asp:TableHeaderCell></asp:TableRow></asp:Table></asp:Gridview>"
Your syntax is a bit off indeed. Remember that everything inside <%# %> should be a valid C#, so 'Label2' is an invalid literal. To fix that, swap quotes over:
text='<%# Eval("Label1") %>'
Update.
Missed the fact that GridView is itself surrounded by single quotes of data-content attribute. Even though this markup looks quite weird, there is a way to make it work, using double quotes everywhere and escaping appropriately:
text="<%# Eval("Label1") %>"
I have a strange problem, but I can't get a solution:
We use the Infragistics WebDataGrid, in which I set a dropdown and a textbox.
For both of them I set a requiredfieldvalidator:
<asp:RequiredFieldValidator ValidationGroup="attrib" runat="server" ControlToValidate="ddlIndRoleAttribType" ToolTip="Attribute Type is not set." InitialValue="-- not selected --" Text="!" Display="Dynamic" />
</div>
</ItemTemplate>
</ig:TemplateDataField>
<ig:TemplateDataField Header-Text="Value" Key="IndRoleAttribVal" Width="50%">
<ItemTemplate>
<div>
<stgwc:TextEdit runat="server" Width="300px" ID="txtIndRoleAttribValue" />
<asp:RequiredFieldValidator ValidationGroup="attrib" runat="server" ControlToValidate="txtIndRoleAttribValue" Width="90%" ToolTip="Attribute Value is not set." Text="!" Display="Dynamic" />
</div>
</ItemTemplate>
</ig:TemplateDataField>
<ig:TemplateDataField Header-Text="Action" Key="IndRoleAttribAction" Width="40px">
<HeaderTemplate>
<stgwc:ImageLinkButton runat="server" ID="btnCreateNewAttribute" CausesValidation="false" ImageUrl="~/Images/base/Icons/16x16/add2.png"
ToolTip="Add new Attribute" CommandName="Create" />
<stgwc:ImageLinkButton runat="server" ID="btnSaveAllAttributes" ValidationGroup="attrib" ImageUrl="~/Images/base/Icons/16x16/disk_green.png"
ToolTip="Save all Attributes" CommandName="Save" />
<stgwc:ImageLinkButton runat="server" ID="btnCancelChanges" CausesValidation="false" ImageUrl="~/Images/base/Icons/16x16/undo.png"
CommandName="Cancel" ToolTip="Discard changes" />
</HeaderTemplate>
<ItemTemplate>
<stgwc:ImageLinkButton runat="server" ID="btnDeleteAttribute" CausesValidation="false" ImageUrl="~/Images/base/Icons/16x16/delete2.png"
ToolTip="Delete this Attribute" CommandName="Delete" CommandArgument='<%# Bind("IR_ATTR_ID") %>' />
</ItemTemplate>
</ig:TemplateDataField>
</Columns>
</stgwc:WebDataGrid>
I do a postback if a new item should be created, then I set it to the datasource and reload it, something like this:
dgrIndRoleAttributes.ClearDataSource();
dgrIndRoleAttributes.Rows.Clear();
dgrIndRoleAttributes.DataSource = null;
dgrIndRoleAttributes.DataSource =
(Session[Constants.Session.RoleAttributes.ToString()] as List<TmpIndRoleAttribute>);
dgrIndRoleAttributes.DataBind();
Now comes the funny part: When I press enter after adding a new row, Firefox (only Firefox, on IE it works proper), seems to think the validators on the validator-group attrib arent set proper and I need to click twice, to make it save.
If I check this with the firebug, I see both validators being on display: none, so this cant be problem. As said, this happens only on Firefox, and I cant reproduce it on a small testsample.
Has anyone a idea, what could cause this behavior? Since I'm not used to this validators, I can't imagine, what could cause this problems.
Thanks for you response!
Matthias
Edith says: It's a infragistics bug... Another one... I use in this case the normal asp textbox and change the style with some css.
I have a formview, connected to an objectDataSource.
It's a pretty easy code in the "EditItemTemplate" of the formview, implemented in the .aspx class:
<EditItemTemplate>
Var1: <asp:TextBox ID="txtVar1" runat="server" Text='<%# Bind("var1") %>' />
Var2: <asp:TextBox ID="txtVar2" runat="server" Text='<%# Bind("var2") %>' />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="ButtonSave_Click" />
</EditItemTemplate>
When pressing "save" the data of "var1" and "var2" passes with the object to the next update method, that is implemented in the business logic class:
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
public void UpdateMyObject(MyObject updatedObject)
{
if (updatedObject.EntityState == EntityState.Detached)
mContext.MyObjects.Attach(updatedObject);
mContext.ObjectStateManager.ChangeObjectState(updatedObject, System.Data.EntityState.Modified);
int numberOfAffectedRows = mContext.SaveChanges();
if (numberOfAffectedRows == 0)
throw new DataNotUpdatedException("No object updated!");
}
The thing is that I need to pass a 3rd variable from the aspx to the business logic, without exposing it to the user through a textbox. It's a Guid and there is no need to change it.
With the current code, the Guid that is assigned to the "updatedObject" is only zeros and not the original Guid of the object, that it has and would have passed if I had binded it to a textbox, using a <%# Bind("entryGuid") %>.
So, my question is how can I pass this Guid (or any other variable) to the business logic layer, without the need to expose it? Is there an elegant way to bind data to the updated object not through a textbox?
Thanks!
Since #tzerb's answer left out an example, I thought I'd provide one.
<EditItemTemplate>
Var1: <asp:TextBox ID="txtVar1" runat="server" Text='<%# Bind("var1") %>' />
Var2: <asp:TextBox ID="txtVar2" runat="server" Text='<%# Bind("var2") %>' />
Var3: <asp:HiddenField id="hdnVar3" runat="server" Value='<%# Bind("var3") %>' />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="ButtonSave_Click" />
</EditItemTemplate>
I think you're looking for
input type="hidden"
This will hide the input from the users and will be available in the post back. The users will be able to see it if they 'view code'.
I want to disable the button that is contained in a data list row. Is it possible to disable the button after it has been clicked once by the user? If so, can someone please suggest how I can achieve that.
<asp:DataList ID="DataList1" runat="server" DataKeyField="Qno" OnItemCommand="DataList1_OnItemCommand"
DataSourceID="SqlDataSource1">
<ItemTemplate> <asp:RadioButton ID="RadioButton1" runat="server" Text='<%# Eval("Ans1") %>' GroupName="qu" />
<br />
<asp:RadioButton ID="RadioButton2" runat="server" Text='<%# Eval("Ans2") %>' GroupName="qu" /> <asp:Button ID="Button2" runat="server" Text="Submit" CommandName="Validate" />
<br />
</ItemTemplate>
I have nothing to do with ASP, but if you want javascript (jQuery) solution then you can just do something like
$(.once-clickable-button).click(function(){
$(this).attr("disabled", "disabled");
// if needed - do what it is supposed to do
});
(of course add class "once-clickable-button" to the button or chose the name that suits you)
<script type="text/javascript">
function checkEnableSubmit() {
document.getElementById("Button2").disabled = true;
}
</script>
call this method on the onclick event when u want to disable the button
With javascript you could do something like the following:
document.getElementById("buttonID").disabled = true
I have a validation summary inside a asp.net formview and the validators just don't seem to trigger the validation summary.
Things I already tried:
Enable the ViewState on everything.
Set the ClientID to static.
Set the ValidationGroup to the same group on both all validators and the summary.
Used Google to search for a solution.
Anyone has any ideas on how to handle this?
Code. Formview declaration:
<asp:FormView ID="FormViewPerson" runat="server" DataSourceID="ObjectDataSourcePerson"
DefaultMode="Edit" OnItemUpdating="FormViewPerson_ItemUpdating" OnItemCommand="FormViewPerson_ItemCommand"
OnItemUpdated="FormViewPerson_ItemUpdated" Width="100%">
Any of the textboxes with the validator:
<td> <asp:TextBox ID="NumberTextBox" runat="server" Text='<%# Bind("Number") %>'
TabIndex="10" CausesValidation="True" ClientIDMode="Static" />
<asp:RequiredFieldValidator ID="RequiredFieldValidatorNumber" runat="server" ControlToValidate="NumberTextBox"
ErrorMessage="Number is Required" ForeColor="Red"
ValidationGroup="EditPerson">*</asp:RequiredFieldValidator>
</td>
And the submitbutton:
<asp:ValidationSummary ID="ValidationSummaryPerson" runat="server"
ForeColor="Red" ClientIDMode="Static" CssClass="validation"
ShowMessageBox="True" ValidationGroup="EditPerson" ViewStateMode="Enabled"
/>
<asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Save" ValidationGroup="EditPerson" CssClass="ButtonStyle" TabIndex="90" />
<asp:Button ID="EditCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel" CssClass="ButtonStyle" TabIndex="100"
ValidationGroup="EditPerson" />
Hope the CausesValidation for the button is set to true.
Well, I fixed the issue. If anyone else encounters this problem try the following:
Ensure there is only one (1) validation group on the entire form.
Set "causesvalidation" to "true" on the submitbutton.
Check, double check and triple check the spelling of your validationgroup property on all relevant controls.
Be on the lookout for javascript errors in your client-side scripts. They can cause havoc.