I want to add comment in the membership table.
I added this in my .aspx code:
<asp:CreateUserWizard ID="Register" runat="server" OnCreatedUser="RegisterUserWithRoles_CreatedUser">
<CreateUserButtonStyle />
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
<ContentTemplate>
<table>
<td align="right">
<asp:Label ID="RoomLabel" runat="server" AssociatedControlID="Room">Room number:</asp:Label>
</td>
<td>
<asp:TextBox ID="Room" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RoomRequired" runat="server" ControlToValidate="Room"
ErrorMessage="Room number is required." ToolTip="Room number is required."
ValidationGroup="Register">*</asp:RequiredFieldValidator>
</td>
...
This is my aspx.cs code:
protected void RegisterUserWithRoles_CreatedUser(object sender, EventArgs e)
{
TextBox comment=
(TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("Comment");
Membership.GetUser(Register.UserName).Comment = comment.Text ;
}
}
If I debug comment.Text= "What I typ in my textbox"
But if I look in the membership table comment is null
I must just use UpdateUser.
Like this:
TextBox comment=
(TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("Comment");
MembershipUser user = Membership.GetUser(Register.UserName);
user.Comment = comment.Text;
Membership.UpdateUser(user);
Related
I want validation like, if i select Status as Reject then there must be some Comments(compulsory). and if status is Accept then comments may be blank (not compulsory)
How I will do it in ASP.NET, Please find my code
<tr>
<td width="30%">
<b>Status:</b>
</td>
<td>
<asp:RadioButton ID="lAccept" runat="server" AutoPostBack="True"
CausesValidation="True" Text="Accept" />
<asp:RadioButton ID="lReject" runat="server" AutoPostBack="True"
CausesValidation="True" Text="Reject " />
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ErrorMessage="Please Select it is Accepted or Rejected" ForeColor="Red"></asp:RequiredFieldValidator>
</tr>
<tr>
<td width="30%">
<b>Qty Rejected:</b>
</td>
<td>
<asp:TextBox ID="lRejectedQty" runat="server" CausesValidation="True"></asp:TextBox>
<%-- <asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="Only interger between 1 to 10000000 " ondisposed="Page_Load"
oninit="Page_Load" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>--%>
<asp:RangeValidator ID="RangeValidator3" runat="server"
ErrorMessage="Rejected Quantity must be in change of 1 to 10,000,000"
ControlToValidate="lRejectedQty" Display="Dynamic" ForeColor="Red"
MaximumValue="10000000" MinimumValue="1"></asp:RangeValidator>
</td>
</tr>
<tr> <td width="30%">
<b>Comments:</b>
</td>
<td>
<TEXTAREA rows=5 cols=40 name="lComments" id="lComments"></TEXTAREA>
</td>
The simplest way for this is to use a CustomValidator.
MSDN: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator(v=vs.110).aspx
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="You must enter Comments if you choose to Reject."
OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>
Then in your code-behind you may do more detailed checks for it being valid or not
protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = true; // set it to be valid by default
if (lReject.Checked == true && string.IsNullOrWhiteSpace(lComments.Text) == true)
{
// Reject was selected and no comments were entered
e.IsValid = false;
}
}
The drawback is that this requires a PostBack to the server and will validate after the other types in most cases.
I have created an aspx page in which I want some controls to be enabled on basis of user selection.
If user selects All two radio buttons should be enabled, hide otherwise.
My declarative part is:
<tr>
<td>
<asp:Label ID="lblCommunityMembers" runat="server" Text="Community Members" />
</td>
<td>
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<asp:UpdatePanel ID="upCommunityMembers" runat="server">
<ContentTemplate>
<asp:RadioButton ID="rdbCommunityMembersAll" runat="server" Text="All" GroupName="grpCommMembers" Checked="true" OnCheckedChanged="rdbCommunityMembersAll_CheckedChanged" AutoPostBack="true" />
<asp:RadioButton ID="rdbCommunityMembersSelectedUsers" runat="server" Text="Selected Users" GroupName="grpCommMembers" OnCheckedChanged="rdbCommunityMembersSelectedUsers_CheckedChanged" AutoPostBack="true" />
<SharePoint:ClientPeoplePicker ID="ppCommunityMembers" runat="server" AllowMultipleEntities="true" AllowEmpty="false" Visible="false" />
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblCommunityCatagory" runat="server" Text="Community Catagory" />
</td>
<td>
<asp:DropDownList ID="ddlCommunityCatagory" runat="server">
<asp:ListItem Value="0">---- Select One ----</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:RequiredFieldValidator ID="rfvCommunityCatagory" runat="server" InitialValue="0" ErrorMessage="Please Select Community Catagory"
ForeColor="Red" ControlToValidate="ddlCommunityCatagory" Display="Dynamic" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblCommunityAccess" runat="server" Text="Required Approval?" Visible="false" />
</td>
<td>
<asp:RadioButton ID="rdbRequiredApprovalYes" runat="server" Text="Yes" GroupName="grpCommMembers" Checked="true" Visible="false" />
<asp:RadioButton ID="rdbRequiredApprovalNo" runat="server" Text="No" GroupName="grpCommMembers" Visible="false"/>
</td>
</tr>
My code behind:
protected void rdbCommunityMembersSelectedUsers_CheckedChanged(object sender, EventArgs e)
{
if (rdbCommunityMembersSelectedUsers.Checked)
{
enableControls();
}
else
{
disableControls();
}
}
protected void rdbCommunityMembersAll_CheckedChanged(object sender, EventArgs e)
{
if (rdbCommunityMembersAll.Checked)
{
disableControls();
}
else
{
enableControls();
}
}
protected void enableControls()
{
ppCommunityMembers.Visible = true;
lblCommunityAccess.Visible = true;
rdbRequiredApprovalNo.Visible = true;
rdbRequiredApprovalYes.Visible = true;
}
protected void disableControls()
{
ppCommunityMembers.Visible = false;
lblCommunityAccess.Visible = false;
rdbRequiredApprovalNo.Visible = false;
rdbRequiredApprovalYes.Visible = false;
}
If Community members are selected to all then "Required Approval?" part should get hidden.
But problem is when I select selected users then I am getting only people picker control visible, the required approval contorls are not getting displayed. What am I missing?
Your Radio Buttons rdbCommunityMembersAll and rdbCommunityMembersSelectedUsers are inside update panel, thus they are doing partial postback. To make the control visible outside the UpdatePanel you can do any one of the following:
Move rdbCommunityMembersAll and rdbCommunityMembersSelectedUsers
outside the UpdatePanel
Move the controls you need to make visible
(lblCommunityAccess, rdbRequiredApprovalNo, rdbRequiredApprovalYes)
inside UpdatePanel
Or
Instead of setting Visible="false", set style="display:none" and
trigger the visibility through javascript/jquery
could you please tell me, why custom validator (created dynamically), information here is not added to validation summary? Is it because of updatepanel? How to make it work?
I am absolutely exhausted, but cannot find appropriate solution...
MultiFreeSet control code-behind:
protected void btnPatternAdder_Click(object sender, EventArgs eventArgs)
{
var includeEventArgs = new IncludeEventArgs();
baseTSMAlertConfigEditControlWithInclude.btnPatternAdder_Click(sender, includeEventArgs);
if (includeEventArgs.Cancel)
{
/*
var ClientValidationFunctionName = string.Format("{0}_ClientValidation", ID);
Page.ClientScript.RegisterClientScriptBlock(
GetType(),
string.Format("{0}_validationScript", ID),
string.Format("function {0}(sender, eventArgs) {{ eventArgs.errormessage = '{1}', eventArgs.IsValid = false; return; }}",
ClientValidationFunctionName,
includeEventArgs.Message));
*/
var customValidator = new CustomValidator
{
//ClientValidationFunction = ClientValidationFunctionName,
ValidationGroup = ValidationGroup,
IsValid = false,
ErrorMessage = includeEventArgs.Message,
Display = ValidationDisplayType
};
Page.Validators.Add(customValidator);
}
}
MultiFreeSet control markup:
<asp:UpdatePanel ID="upFreeSet" runat="server">
<ContentTemplate>
...
<asp:PlaceHolder runat="server" ID="phIncludePattern" Visible="false">
<tr>
<td class="SubHead">[IncludeCaption]</td>
<td>
<asp:TextBox ID="txtIncludePattern" runat="server" Text="" CssClass="MediumTextBox" />
</td>
<td>
<asp:Button CommandName="ListAdder" ID="btnPatternAdder" runat="server" CssClass="buttonClass displayBlock" Text="Add" OnClick="btnPatternAdder_Click" />
</td>
</tr>
<tr>
<td class="SubHead ta">[IncludedCaption]</td>
<td>
<asp:ListBox ID="lboxIncludePattern" runat="server" SelectionMode="Multiple" Rows="7" CssClass="LargeDropDownList" />
<asp:ObjectDataSource ID="odsIncludePattern" runat="server" />
</td>
<td class="ta">
<asp:Button CommandName="ListDeleter" ID="btnPatternDeleter" runat="server" CssClass="buttonClass displayBlock" Text="Delete selected" OnClick="btnPatternDeleter_Click" />
</td>
</tr>
</asp:PlaceHolder>
...
</ContentTemplate>
Main control markup:
<asp:FormView>
<EditItemTemplate>
...
<ac:multiFreeSet ID="multiNodePatternInclusions" ValidationGroup="vgFrmConfigEdit" IgnoreCase="True" runat="server" Caption="Include node name patterns" IncludeCaption="Add node name pattern" IncludedCaption="Included node name patterns" />
<XXX:SaveButton ID="btnImgSave" runat="server" ValidationGroup="vgFrmConfigEdit" />
<XXX:CancelBackButton ID="btnImgCancel" runat="server" />
<asp:ValidationSummary ID="valSummary" runat="server" ValidationGroup="vgFrmConfigEdit" CssClass="NormalRed" ShowSummary="True" />
...
</EditItemTemplate>
</asp:FormView>
OK. Well.. I had to create a separate validation summary with separate validation group inside update panel..
So i have the following method
protected void isDirector_CheckedChanged(object sender, EventArgs e)
{
HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("today");
But getting error
CS0117: 'System.EventArgs' does not contain a definition for 'Item'
EDIT :
<asp:UpdatePanel ID="UpdatePanel2"
runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<asp:RadioButtonList ID="isDirector" RepeatDirection="Horizontal" runat="server" AutoPostBack="True" OnSelectedIndexChanged="isDirector_CheckedChanged">
<asp:ListItem Text="Yes" Value="True" selected></asp:ListItem>
<asp:ListItem Text="No" Value="False"></asp:ListItem>
</asp:RadioButtonList>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel
<ContentTemplate>
<tr runat="server" id="test">
<td>Director First Name:</td>
<td><asp:TextBox ID="DirectorfirstNametxt" runat="server" MaxLength="100" CssClass="input"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" Display="None" runat="server"
ErrorMessage="Director First Name is required." ControlToValidate="DirectorfirstNametxt"></asp:RequiredFieldValidator>
</td>
</tr>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="isDirector" EventName="SelectedIndexChanged" />
</Triggers>
I am trying to change the CSS CLASS of TR ID = "test"
Assuming you have your Checkboxes inside of a HTML-TableRow and you want to set the CSS-Class of the TR in the CheckedChanged-Event:
This is an example(note that the TR's have a runat="server"-tag):
<table>
<tr ID="TR1" runat="server">
<td>
<asp:CheckBox ID="CheckBox1" OnCheckedChanged="isDirector_CheckedChanged" AutoPostBack="true" runat="server" />
</td>
</tr>
<tr ID="TR2" runat="server">
<td>
<asp:CheckBox ID="CheckBox2" OnCheckedChanged="isDirector_CheckedChanged" AutoPostBack="true" runat="server" />
</td>
</tr>
<tr ID="test" runat="server">
<td>
<asp:CheckBox ID="CheckBox3" OnCheckedChanged="isDirector_CheckedChanged" AutoPostBack="true" runat="server" />
</td>
</tr>
</table>
and this is the codebehind:
protected void isDirector_CheckedChanged(object sender, EventArgs e)
{
//var row = (HtmlTableRow)((CheckBox)sender).Parent.Parent;
test.Attributes("class") = "CssClass";
}
Edit: if your tr's are runat="server" and they have unique ID's, you can access them directly
What kind of control is isDirector_CheckChanged tied to - a checkbox?
As the error says, EventArgs is the type of event you're expecting and it doesn't have an 'Item' property. Maybe you're thinking of GridView, Repeater, or other 'item-like' controls?
I'm guessing you're trying to handle the changed event of a checkbox you've put in a repeating/table control. If so, you'll need to handle a Selected or similar event for the repeater that DOES use an EventArgs-derived type with an Item property.
I am working with the listview control. By default I am showing the itemtemplate with an edit button. When the edit button is pressed the listview switches to the edititemtemplate. I need to populate one of the controls in the edititemtemplate based on the item being edited - I've tried accessing the control (via FindControl) in the ItemEditing event (and pretty much every other event as well), however the controls just don't seem to exist. I can access controls in the itemtemplate ok, but not the edititemtemplate.
Can anyone let me know how I can access a control held within the edititemtemplate of a listview, and from which event I should do so?
EDIT
I'm trying to access the control using this:
protected void UnitsLV_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListViewItem item = UnitsLV.Items[e.NewEditIndex];
ListBox tempLB = (ListBox)e.item.FindControl("ListBox3");
}
I've also tried in ItemDataBound and ItemCreated.
The listview declaration is:
<asp:Content ID="Content1" ContentPlaceHolderID="ColumnA" runat="server">
<asp:Panel ID="Panel1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="SummaryPnl" runat="server">
<asp:ListView ID="UnitsLV" runat="server" DataSourceID="DataLDS" DataKeyNames="u_uid"
InsertItemPosition="LastItem" OnItemInserting="UnitsLV_ItemInserting" OnItemDataBound="UnitsLV_ItemDataBound"
OnItemCreated="UnitsLV_ItemCreated" onitemediting="UnitsLV_ItemEditing">
<ItemTemplate>
<tr class="rowA">
<td>
<asp:Label runat="server" ID="UnitIDLbl" Text='<%# Eval("u_uid")%>'></asp:Label>
</td>
<td>
<%# Eval("u_Title")%>
</td>
<td>
<asp:LinkButton ID="EditBtn" runat="server" CommandName="Edit" CommandArgument='<%#Eval("u_uid") %>'
Text="Edit" />
</td>
<td>
<asp:LinkButton ID="DeleteBtn" runat="server" CommandName="Delete" CommandArgument='<%#Eval("u_uid") %>'
Text="Delete" />
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr class="rowB">
<td>
<br />
</td>
<td>
<br />
<asp:TextBox ID="TitleTB" runat="server" Text='<% #Bind("u_Title")%>'></asp:TextBox>
</td>
<td>
<br />
<asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
<asp:ListBox ID="ToBeDeletedLB" runat="server"></asp:ListBox>
</td>
<td>
<asp:LinkButton ID="InsertBtn" runat="server" CommandName="Insert" Text="Insert" />
</td>
<td>
<asp:LinkButton ID="CancelBtn" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
</tr>
</InsertItemTemplate>
<EditItemTemplate>
<tr class="rowB">
<td>
<br />
<asp:Label runat="server" ID="UnitIDLbl" Text='<%# Bind("u_uid")%>'></asp:Label>
</td>
<td>
<br />
<asp:TextBox ID="TitleTB" runat="server" Text='<% #Bind("u_Title")%>'></asp:TextBox>
</td>
<td>
<br />
<asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
<asp:ListBox ID="ToBeDeletedLB" runat="server"></asp:ListBox>
</td>
<td>
<asp:LinkButton ID="UpdateBtn" runat="server" CommandName="Update" Text="Update" />
</td>
<td>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
</tr>
</EditItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server" width="100%">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="" width="100%">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</td>
</tr>
<tr id="Tr2" runat="server">
<td id="Td2" runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</asp:Content>
EDIT:
I've iterated over all the controls in the listview using code similar to below, and the control is still not visible. Does the ItemEditing event fire before the edit template is shown? If so, what event can I use to access the edit template controls?
foreach (Control child in control.Controls)
{
Control result = Find(child, id);
if (result != null)
{
return result;
}
}
**EDIT: **
I can access the controls in the edititemtemplate in the listview's ItemCreated event, however none they have no content (I'd assume the data hasn't been bound yet), so I can't get the key-value I need to do a lookup to get the data I need to populate the control.
I've figured out a way to do what I need to do, though I'm not terribly happy with it.
protected void UnitsLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (UnitsLV.EditIndex > -1)
{
// Controls within the edititemtemplate are available via e.Item.FindControl("controlname")
}
}
I don't know about previous editions but in Visual Studio 2010 you can easily access the edit item trhough the "ListView.EditItem" property like this:
private void myListView_ItemEditing(object sender, ListViewEditEventArgs e)
{
myListView.EditIndex = e.NewEditIndex;
myListView.DataBind();
ListViewItem lvwItem = lvwLista.EditItem;
ListBox tempLB = (ListBox) lvwItem.FindControl("ListBox3");
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
ListBox myListBox = (ListBox)(((ListView)sender).EditItem.FindControl("ListBox1"));
}
protected void UnitsLV_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListViewItem item = UnitsLV.Items[e.NewEditIndex];
ListBox tempLB = (ListBox)e.item.FindControl("ListBox3");
}
I believe I found a typo in the above function. The second line should be
ListBox tempLB = (ListBox)item.FindControl("ListBox3");
What I did was replace "e.item" with "item"
I usually use the ItemDataBound Event... check the other options in ListItemType Enum
protected void UnitLV_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
ListBox myListBox = (ListBox) e.Item.FindControl("ListBox3");
}
}
this is similar to the accepted answer, but I think its author was really driving towards this:
protected void UnitsLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem listViewDataItem = e.Item as ListViewDataItem;
if (UnitsLV.EditIndex == listViewDataItem.DataItemIndex)
{
// Controls within the edititemtemplate are available via e.Item.FindControl("controlname")
}
}