fill controls of web user control from parent page - c#

i have two web user control in one the grid view is showing the result and the other contains the simply textboxes and the dropdowns that are basically created to edit that gridview items
the aspx.cs page code is
GridView gView = PlaceHolder2.Controls[0].FindControl("grvBranches") as GridView;
int index = gView.SelectedIndex;
GridViewRow grd = gView.Rows[index];
Label lbl = (Label)grd.FindControl("lblID");
int rowid = 0;
int.TryParse(lbl.ToString(), out rowid);
In rowid i contains the id of that item which is selected
now i want to fill all the following items in webusercontrol
<div class="col2_form1">
<div class="form1_bar1">
<div class="form1_txt1_div">
<asp:Label ID="lblBranchName" runat="server" Text="Branch Name">
</asp:Label>
</div>
<div class="form1_txtfield_div">
<asp:TextBox runat="server" ID="txtBranchName" class="form1_TxtField" />
</div>
<div class="form1_txt2_div">
<asp:RequiredFieldValidator ID="RFVBranchName" ErrorMessage="*" Display="Dynamic"
ValidationGroup="Save" ControlToValidate="txtBranchName" runat="server" />
<%-- <asp:Label Text="Error" ID="lblBranchNameError" runat="server" />--%>
</div>
</div>
<div class="form1_bar2">
<div class="form1_txt1_div">
<asp:Label Text=" Organization Name" ID="lblOrganizationName" runat="server" />
</div>
<div class="form1_txtfield_div">
<asp:DropDownList ID="ddlOrganization" CssClass="form1_TxtField" runat="server">
</asp:DropDownList>
</div>
<div class="form1_txt2_div">
<%--<asp:Label Text="Error" ID="lblOrganizationNameError" runat="server" />--%>
</div>
</div>
<div class=" form1_bar1">
<div class="form1_txt1_div ">
<asp:Label Text="Address Line1" ID="lblAddresLine1" runat="server" />
</div>
<div class=" form1_txtfield_div">
<asp:TextBox runat="server" ID="txtAddresLine1" CssClass="form1_TxtField" />
</div>
<div class="form1_txt2_div ">
<asp:RequiredFieldValidator ErrorMessage="*" Display="Dynamic" ValidationGroup="Save"
ControlToValidate="txtAddresLine1" runat="server" />
</div>
</div>
<div class=" form1_bar2">
<div class="form1_txt1_div ">
<asp:Label Text="Address Line2" ID="lblAddresLine2" runat="server" />
</div>
<div class="form1_txtfield_div ">
<asp:TextBox runat="server" ID="txtAddresLine2" CssClass="form1_TxtField" />
</div>
<div class="form1_txt2_div ">
</div>
</div>
<div class="form1_bar1 ">
<div class="form1_txt1_div">
<asp:Label Text="Country" ID="lblCountry" runat="server" />
</div>
<div class="form1_txtfield_div " style="float: left;">
<asp:DropDownList ID="ddlCountry" AutoPostBack="true" CssClass="form1_TxtField" runat="server"
OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">
</asp:DropDownList>
</div>
<div class=" form1_txt2_div">
</div>
</div>
<div class=" form1_bar2">
<div class="form1_txt1_div ">
<asp:Label Text="State" ID="lblState" runat="server" />
</div>
<div class="form1_txtfield_div ">
<asp:DropDownList ID="ddlState" AutoPostBack="true" CssClass="form1_TxtField" runat="server"
OnSelectedIndexChanged="ddlState_SelectedIndexChanged">
</asp:DropDownList>
</div>
<div class="form1_txt2_div ">
</div>
</div>
<div class="form1_bar1 ">
<div class="form1_txt1_div ">
<asp:Label Text="City" ID="lblCity" runat="server" />
</div>
<div class="form1_txtfield_div ">
<asp:DropDownList runat="server" CssClass="form1_TxtField" ID="ddlCity">
</asp:DropDownList>
</div>
<div class="form1_txt2_div ">
</div>
</div>
<div class="form1_bar2 ">
<div class="form1_txt1_div ">
</div>
<div class="form1_txtfield_div">
<asp:Button Text="Save" Width="100px" runat="server" ID="btnSave" ValidationGroup="Save"
OnClick="btnSave_Click" />
</div>
<div class="form1_txt2_div">
<asp:Label Text="" ID="lblMsg" Visible="false" runat="server" />
</div>
</div>
i have a property on the webusercontrol as
public string ABC_DEF
{
set { txtABC.Text = value; }
get { return txtABC.Text; }
}
and on the parent page i.e on the aspx page i call it as
if (ViewState["controlname"] != null)
{
PlaceHolder pl = MainPanel.FindControl(ViewState["controlname"].ToString()) as PlaceHolder;
pl.Controls.RemoveAt(0);
}
Control uc = (Control) Page.LoadControl("~/usercontrols/control_forms/branch_aspx_form.ascx");
ViewState["path"] = "~/usercontrols/control_forms/branch_aspx_form.ascx";
ViewState["controlname"] = "PlaceHolder1";
PlaceHolder1.Controls.Add(uc);
branch_view[] branch_to_edit = EntityService.ServiceInstance.GetBranchByBranchID(row_id);
foreach (branch_view item in branch_to_edit)
{
usercontrols.control_forms.branch_aspx_form branch_control =
new usercontrols.control_forms.branch_aspx_form();
branch_control.ABC_DEF = " hello"; // Call property here
}
thanks
Gurbax

In your user control .ascx file create properties for each of the fields you need to be pulled onto the user control,
Then you can simply access them from the .aspx page by specifying the UserControlId.Property syntax like so:
public string Name
{
get { return txtName.Text; }
set { txtName.Text = value; }
}
public string Surname
{
get { return txtSurname.Text; }
set { txtSurname.Text = value; }
}
On the .aspx page the properties can be accessed like this:
protected void PopulateUserControl_Click(object sender, EventArgs e)
{
userControlName.Name = "Bob";
userControlName.Surname = "The Builder";
}

Related

Is there a way to resolve a problem of "SelectedIndex which is invalid" in c#

I am a first year student and I am currently developing a web application base on an ecommerce website. I recently encountered a problem which I cannot resolve while spending weeks on it.
The problem is when I update a product on the app I get an error this says crashes my application.
Here is the Problem.
Exception Details: System.ArgumentOutOfRangeException: 'ddlcat' has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value
and here my code for the frontend and backend.
frontend:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="maincontent" runat="server">
<div class="form-horizontal">
<h4>Manage product</h4>
<hr />
<div class="col-md-8">
<asp:TextBox runat="server" ID="txtProductid" Visible="false"
CssClass="form-control" />
</div>
<div class="form-group row justify-content-center">
<asp:Label runat="server" CssClass="col-md-2 col-form-label">Select
a Category</asp:Label>
<div class="col-md-8">
<asp:DropDownList ID="ddlcat" runat="server"
CssClass="form-control">
</asp:DropDownList>
</div>
</div>
<div class="form-group row justify-content-center">
<asp:Label runat="server"
CssClass="col-md-2 col-form-label"></asp:Label>
</div>
<div class="form-group row justify-content-center">
<asp:Label runat="server" CssClass="col-md-2 col-form-label">Product name</asp:Label>
<div class="col-md-8">
<asp:TextBox runat="server" ID="txtPname"
CssClass="form-control" />
</div>
</div>
<div class="form-group row justify-content-center">
<asp:Label runat="server"
CssClass="col-md-2 col-form-label">Product Brand</asp:Label>
<div class="col-md-8">
<asp:TextBox runat="server" ID="txtPbrand"
CssClass="form-control" />
</div>
</div>
<div class="form-group row justify-content-center">
<asp:Label runat="server" CssClass="col-md-2 col-form-label">Product price</asp:Label>
<div class="col-md-8">
<asp:TextBox runat="server" ID="txtprice"
CssClass="form-control" />
</div>
</div>
<div class="form-group row justify-content-center">
<asp:Label runat="server"
CssClass="col-md-2 col-form-label">Product Description</asp:Label>
<div class="col-md-8">
<asp:TextBox runat="server" ID="txtPdesc" TextMode="Multiline"
CssClass="form-control" />
</div>
</div>
<div class="form-group row justify-content-center">
<asp:Label runat="server"
CssClass="col-md-2 col-form-label">Product image</asp:Label>
<div class="col-md-8">
<asp:FileUpload ID="fuimage" runat="server" CssClass="" />
<asp:Image ID="Image1" runat="server" Visible="false" Width="75"
Height="100" />
</div>
</div>
<div class="form-group row justify-content-center">
<asp:Label runat="server"
CssClass="col-md-2 col-form-label"></asp:Label>
<div class="col-md-8">
<div class="form-check-inline">
<asp:CheckBox runat="server" ID="chkstatus"
CssClass="form-check-input" />
<asp:Label runat="server"
CssClass="form-check-label">Status</asp:Label>
</div>
</div>
</div>
<div class="form-group row justify-content-center">
<div class="offset-md-2 col-md-8">
<asp:Button runat="server" ID="btnupdate" OnClick="btnupdate_Click" Text="Update Product"
Visible="false" CssClass="btn btn-block btn-outline-primary" />
<asp:Button runat="server" ID="btndelete" OnClick="btndelete_Click" Text="Delete Product"
OnClientClick="return confirm ('Are you sure you want to delete?')"
Visible="false" CssClass="btn btn-block btn-outline-primary" />
<asp:Button runat="server" ID="btncancel" OnClick="btncancel_Click" Text="Cancel"
Visible="false" CssClass="btn btn-block btn-outline-primary" />
<asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
</div>
</div>
</div>
<hr />
<asp:GridView ID="gvs" AutoGenerateColumns="false"
DataKeyNames="P_id"
OnSelectedIndexChanged="gvs_SelectedIndexChanged" ClientIDMode="Static"
Width="800" runat="server">
<HeaderStyle BackColor="#eeeeee" ForeColor="White" Font-Bold="true"
Height="30" />
<AlternatingRowStyle BackColor="#f5f5f5" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnSelect" runat="server"
CssClass="btn btn-outline-info" CommandName="Select" Text="Select" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Movie Name">
<ItemTemplate>
<!-- display the movie name -->
<asp:Label ID="lblproductname" Text='<%#Eval("P_name")%>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
<%-- add an imagefield here to display the poster--%>
</Columns>
</asp:GridView>
</asp:Content>
Backend
protected void gvs_SelectedIndexChanged(object sender, EventArgs e) <----------- theproblem is found here --------------------->
{
lblMsg.Text = "";
Image1.Visible = true;
txtProductid.Text =
(gvs.DataKeys[gvs.SelectedIndex].Value.ToString());
txtPname.Text =
((Label)gvs.SelectedRow.FindControl("lblproductname")).Text;
SqlConnection con = new SqlConnection(_conString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
//create the movieid parameter
cmd.Parameters.AddWithValue("#pid", txtProductid.Text);
//assign the parameter to the sql statement
cmd.CommandText = "SELECT * FROM tblProduct where P_id = #pid";
SqlDataReader dr;
con.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
//retrieving FIELD values and assign the form controls
ddlcat.SelectedIndex = Convert.ToInt32(dr["Category_cat_id"]);
txtPbrand.Text = dr["P_brand"].ToString();
txtPdesc.Text = dr["P_description"].ToString();
txtprice.Text = dr["P_cost"].ToString();
chkstatus.Checked = (Boolean)dr["P_status"];
Image1.ImageUrl = "../image/" + dr["P_image"].ToString();
}
con.Close();
btnupdate.Visible = true;
btndelete.Visible = true;
btncancel.Visible = true;
}
private void ResetAll()
{
btncancel.Visible = false;
btndelete.Visible = false;
btnupdate.Visible = false;
ddlcat.SelectedIndex = 0;
txtPname.Text = "";
txtprice.Text = "";
txtPbrand.Text = "";
txtPdesc.Text = "";
chkstatus.Checked = false;
Image1.ImageUrl = "";
}
Simply if you guys can take a look about it, this would help me greatly.
Thanks in advance

remove row from repeater using data table row index

<asp:Repeater ID="RptSCart" runat="server" OnItemCommand="RptSCart_ItemCommand" Visible="false">
<HeaderTemplate>
<div class="row text-center align-items-center" style="background-color: pink; height: 70px; color: black">
<div class="col-md-2"></div>
<div class="col-md-2">
<p>Product</p>
</div>
<div class="col-md-2">
<p>Price</p>
</div>
<div class="col-md-2">
<p>Stitch Cost</p>
</div>
<div class="col-md-1">
<p>Quantity</p>
</div>
<div class="col-md-2">
<p>Total</p>
</div>
<div class="col-md-1"></div>
</div>
<br />
</HeaderTemplate>
<ItemTemplate>
<div class="row text-center align-items-center">
<div class="col-md-2 ">
<asp:Image ID="PImage" runat="server" ImageUrl='<%#Bind("Pic")%>' CssClass="img image-prod" Width="100" Height="150" />
</div>
<div class="col-md-2">
<asp:Label ID="LabelPName" runat="server" CssClass="product-name" Text='<%#Bind("PName")%>' ForeColor="Black"></asp:Label>
</div>
<div class="col-md-2">
<asp:Label ID="LabelPPrice" runat="server" CssClass="price" Text='<%#Bind("SPrice") %>' ForeColor="Black"></asp:Label>
</div>
<div class="col-md-2">
<asp:Label ID="lblSPrice" runat="server" CssClass="price" Text='<%#Bind("STPrice") %>' ForeColor="Black"></asp:Label>
</div>
<div class="col-md-1">
<asp:Label ID="Label1" runat="server" CssClass="price" Text="1" ForeColor="Black"></asp:Label>
</div>
<div class="col-md-2">
<asp:Label ID="lblTotal" CssClass="total" runat="server" Text='<%#Bind("Amount") %>' ForeColor="Black"></asp:Label>
</div>
<div class="col-md-1">
<asp:LinkButton ID="lnkRemove" runat="server" CssClass="product-remove" CommandName="Delete"><span class="ion-ios-close-circle"></span></asp:LinkButton>
</div>
</div>
<h1>
<hr />
</h1>
</ItemTemplate>
</asp:Repeater>
DataTable dt1 = (DataTable)Session["Stitch_Cart"];
RptSCart.DataSource = dt1;
RptSCart.DataBind();
RptSCart.Visible = true;
Here is my repeater in which i bind data (products added in cart by customer) from data table. On click of remove button i want to delete row from data table using data table row index instead of repeater index.If quantity of product(xyz) is 2 i am creating two rows in cart and data table (it is according to my requirement). If user wants to remove one, the other one should not be deleted.

How to hide a panel by clicking on dropdown menu?

I have a dropdown menu and it has a set of items.I want to hide a particular panel when i am clicking on a dropdown menu.
Below is my c# code and my panel is designed in asp.net.
I want to disable panel XII when i was clicking on dropdown list item Not Applicable in panel X
protected void ddsscboard_SelectedIndexChanged(object sender, EventArgs e)
{
try {
string sscpanel = ddsscboard.SelectedItem.Text;
panelshow(sscpanel);
}
catch(Exception ex)
{
}
}
public void panelshow(string sscpanel)
{
if (sscpanel == "Not Applicable")
XII.Visible = false;
else
lblmsg.Text = sscpanel;
}
}
panel:-
Class X
<div class="form-group">
<label>Select Board</label>
<asp:DropDownList ID="ddsscboard" CssClass="form-control" OnSelectedIndexChanged="ddsscboard_SelectedIndexChanged" runat="server">
<asp:ListItem>Not Applicable</asp:ListItem>
<asp:ListItem>SSC</asp:ListItem>
<asp:ListItem>CBSC</asp:ListItem>
<asp:ListItem>ICSC</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
</div>
<div class="form-group">
<label>Complition Month</label>
<asp:TextBox ID="txtssccomplitionmonth" CssClass="form-control" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<label>Complition Year</label>
<asp:TextBox ID="txtssccomplitionyear" CssClass="form-control" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<label>Marks Obtained</label>
<asp:TextBox ID="txtsscmarksobtained" CssClass="form-control" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<label>Total Marks</label>
<asp:TextBox ID="txtssctotalmarks" CssClass="form-control" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<label>Upload File</label>
<asp:FileUpload ID="sscfile" runat="server" />
</div>
</div>
<!-- /.col-lg-6 (nested) -->
</div>
<!-- /.row (nested) -->
</div>
</asp:Panel>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<div class="col-lg-12">
<asp:Panel ID="XII" runat="server" CssClass="panel panel-default">
<div class="panel-heading">
Class XII
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Select Board</label>
<asp:DropDownList ID="ddhscboard" CssClass="form-control" runat="server">
<asp:ListItem>Not Applicable</asp:ListItem>
<asp:ListItem>HSC</asp:ListItem>
<asp:ListItem>CBSC</asp:ListItem>
<asp:ListItem>ICSC</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
</div>
<div class="form-group">
<label>Complition Month</label>
<asp:TextBox ID="txthsccomplitionmonth" CssClass="form-control" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<label>Complition Year</label>
<asp:TextBox ID="txthsccomplitionyear" CssClass="form-control" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<label>Marks Obtained</label>
<asp:TextBox ID="txthscmarksobtained" CssClass="form-control" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<label>Total Marks</label>
<asp:TextBox ID="txthsctotalmarks" CssClass="form-control" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<label>Upload File</label>
<asp:FileUpload ID="hscfile" runat="server" />
</div>
</div>
<!-- /.col-lg-6 (nested) -->
</div>
<!-- /.row (nested) -->
</div>
</asp:Panel>
<!-- /.panel-body -->
</div>
You need to set the AutoPostBack attribute in your DropDownList to true. Note you may also want to establish a blank ListItem so "Not Applicable" is not Selected by default:
<asp:DropDownList AutoPostBack="true" ID="ddsscboard" CssClass="form-control" OnSelectedIndexChanged="ddsscboard_SelectedIndexChanged" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Not Applicable</asp:ListItem>
<asp:ListItem>SSC</asp:ListItem>
<asp:ListItem>CBSC</asp:ListItem>
<asp:ListItem>ICSC</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>

Asp:Button not firing in bootstrap Tab Panes

I have set a bootstrap tab pane, within this tab pane there is a form, on asp:Button press the code will not execute.
I know the code works as when not in the tab pane it works every time.
But its for a user control page and would be better to provide function within the tab panes.
This is for all buttons within the tab pane.
if one gets solved then the method to repair others is made!
Here is the HTML markup:
<div class="tab-content col-lg-10">
<div class="tab-pane active" id="pane1">
<%--Content goes in here, so tables, and whatever else--%>
<h3>My Bookings</h3>
<div class="container">
<asp:GridView runat="server" ID="dgBookingView" AutoGenerateColumns="False" DataSourceID="myBookings" CssClass="table table-striped table-bordered" UseAccessibleHeader="True">
<Columns>
<asp:BoundField DataField="BookingID" HeaderText="Booking #" InsertVisible="False" SortExpression="BookingID" />
<asp:BoundField DataField="FilmName" HeaderText="Movie" SortExpression="FilmName" />
<asp:BoundField DataField="Requirements" HeaderText="Requirements" SortExpression="Requirements" />
<asp:BoundField DataField="Showtime" HeaderText="Time" SortExpression="Showtime" DataFormatString="{0:t}" />
<asp:BoundField DataField="ShowDate" HeaderText="Date" SortExpression="ShowDate" DataFormatString="{0:d}" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="myBookings" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT Booking.BookingID, Booking.CustomerFK, Booking.AuditorumFK, Booking.Requirements, Film.FilmName, Showtime.Showtime, Showtime.ShowDate, Showtime.AuditoriumID, Customers.UserNameFK FROM (((((Booking INNER JOIN Seat ON Booking.BookingID = Seat.BookingFK) INNER JOIN Auditorium ON Seat.AuditoriumFK = Auditorium.AuditoriumID) INNER JOIN Showtime ON Auditorium.AuditoriumID = Showtime.AuditoriumID) INNER JOIN Film ON Showtime.FilmFK = Film.FilmID) INNER JOIN Customers ON Booking.CustomerFK = Customers.CustomerID)
WHERE([Customers.UserNameFK] = ? )">
<SelectParameters>
<asp:SessionParameter
Name="userNamePa"
SessionField="userName"
DefaultValue="userName" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<div class="row">
<h3>Specify any Special Requirements</h3>
<div class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">Booking #</label>
<div class="col-sm-6">
<asp:TextBox runat="server" CssClass="form-control" ID="txtBookingID"></asp:TextBox>
</div>
<div class="col-sm-4">
<h4>
<asp:RequiredFieldValidator ID="rqfUserNameValid" ControlToValidate="txtBookingID" runat="server"
ErrorMessage="Enter a Booking Number" CssClass="label label-danger">
</asp:RequiredFieldValidator>
</h4>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Extra Requirements</label>
<div class="col-sm-6">
<asp:TextBox ID="txtRequirements" class="form-control" Rows="5" runat="server"></asp:TextBox>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-6">
<asp:Button runat="server" CssClass="btn btn-default" Text="Make Change" ID="btnRequirements" OnClick="btnRequirements_OnClick" />
</div>
</div>
</div>
</div>
</div>
<div class="tab-pane" id="pane2" runat="server">
<%--Change User Password START--%>
<h3>Change Password</h3>
<div class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">Current Password</label>
<div class="col-sm-6">
<asp:TextBox runat="server" CssClass="form-control" ID="txtCurrentPass" TextMode="Password"></asp:TextBox>
</div>
<div class="col-sm-4">
<h4>
<asp:RequiredFieldValidator runat="server" CssClass="label label-danger" ID="rqfCurrentPassword" ControlToValidate="txtCurrentPass"
ErrorMessage="Enter Current Password">
</asp:RequiredFieldValidator>
</h4>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">New Password</label>
<div class="col-sm-6">
<asp:TextBox runat="server" CssClass="form-control" TextMode="Password" ID="txtNewPassword"></asp:TextBox>
</div>
<div class="col-sm-4">
<h4>
<asp:RequiredFieldValidator runat="server" CssClass="label label-danger" ID="rqfNewPassword" ControlToValidate="txtNewPassword"
ErrorMessage="Required">
</asp:RequiredFieldValidator>
</h4>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Confirm Password</label>
<div class="col-sm-6">
<asp:TextBox runat="server" CssClass="form-control" TextMode="Password" ID="txtConfirmPassword"></asp:TextBox>
</div>
<div class="col-sm-4">
<h4>
<asp:RequiredFieldValidator runat="server" CssClass="label label-danger" ID="rqfConfirmPassword" ControlToValidate="txtConfirmPassword"
ErrorMessage="Required">
</asp:RequiredFieldValidator>
</h4>
<h4>
<asp:CompareValidator runat="server" ControlToValidate="txtConfirmPassword" ControlToCompare="txtNewPassword" ID="PassCompare" ErrorMessage="Confirm password must match password" CssClass="label label-danger"></asp:CompareValidator>
</h4>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-6">
<asp:Button runat="server" CssClass="btn btn-default" Text="Change Password" ID="btnPasswordChange" OnClick="btnChangePassword_OnClick"></asp:Button>
</div>
</div>
</div>
<div class="col-sm-4">
<h4>
<asp:Label runat="server" CssClass="label label-danger" ID="lblPassChange" Visible="False" Text=""></asp:Label>
</h4>
</div>
</div>
<%--Change User Password--%>
</div>
</div>
And the C# Code for button Event:
protected void btnChangePassword_OnClick(object sender, EventArgs eventArgs)
{
//Changes Password of current Logged in User
string oldPass = txtCurrentPass.Text;
string newPass = txtNewPassword.Text;
MembershipUser user = Membership.GetUser(User.Identity.Name);
if (txtCurrentPass.Text == txtNewPassword.Text)
{
lblPassChange.Visible = true;
lblPassChange.Text = "Old Password must not match New Password";
}
else
{
if (user != null && user.ChangePassword(oldPass, newPass))
{
lblPassChange.Visible = true;
lblPassChange.Text = "Password Changed Successfully";
lblPassChange.CssClass = "label label-success";
}
else
{
lblPassChange.Visible = true;
lblPassChange.Text = "Error, Please try again";
}
}
}
Where am i going wrong?
Any help would be appreciated

Alertify.JS and ASP.NET: Call Javascript from Code-behind

I have a question.
I am now using the current version of Alertify.js found here. I wanted to integrate it with my ASP.NET application. When I click the Save button from my Modal Dialog, it proceeds to the code-behind without problems. I've tried "alert("Test");" inside the ScriptManager.RegisterClientScriptBlock and it does render.
Now when I try to use my custom function located in one of the files I've added, it seems the script does not respond, i.e. Alertify does not show.
Here is the JavaScript code from my custom JS file, my C# Code behind for that Save button, and the ASP.NET Page.
JS
function callFeedbackMessage(inputType, inputText) {
var inputFeedback = inputType;
e.preventDefault();
if (inputType == 'Error') {
alertify.error(inputText);
return false;
}
else if (inputType == 'Success') {
alertify.success(inputText);
return false;
}
else {
alertify.log(inputText, "", 10000);
return false;
}
}
C# Code Behind of lnkSave_Click
protected void lnkSave_Click(object sender, EventArgs e)
{
Category newCategory = new ProgrammersToolkitLibrary.BOM.Category();
bool isSuccess = true;
newCategory.CategoryName = txtCategoryName.Text;
newCategory.Description = txtDescription.Text;
isSuccess = CategoryController.CreateNewCategory(newCategory);
if (isSuccess == true)
{
grdCategoriesList.DataBind();
upCategoriesList.Update();
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "alert", "callFeedbackMessage('Success', 'Okay');", true);
}
else
{
}
}
ASP.NET Page
<%# Page Title="" Language="C#" MasterPageFile="~/Layout/Layout.Master" AutoEventWireup="true" CodeBehind="Category_List.aspx.cs" Inherits="ProgrammersToolkit.Views.Maintenance.Categories.Category_List" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div style ="text-align:left;">
<h3>Article Categories</h3>
<div class="btn-group">
<button class="btn btn-default" data-toggle="modal" data-target="#AddCategoryModal"><span class="glyphicon glyphicon-plus"></span> Add Category</button>
<asp:LinkButton ID="LinkButton2" runat="server" CssClass="btn btn-default"><img src="../../../Layout/img/Icon_Excel.gif" /> Export to Excel</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CssClass="btn btn-default"><img src="../../../Layout/img/Icon_Excel.gif" /> Import from Excel</asp:LinkButton>
</div>
</div>
<br /><br />
<div class="panel panel-default">
<div class="panel-heading">
<div class="row">
<div class ="col-lg-6">
<div class="input-group">
<asp:TextBox ID="txtSearch" runat="server" CssClass="form-control"></asp:TextBox>
<span class="input-group-btn">
<asp:LinkButton ID="lnkSearch" runat="server" CssClass="btn btn-default">Search</asp:LinkButton>
<asp:LinkButton ID="lnkReset" runat="server" CssClass="btn btn-default">Reset</asp:LinkButton>
</span>
</div>
</div>
<div class ="col-lg-6"></div>
</div>
</div>
<div class="panel-body">
<asp:UpdatePanel ID="upCategoriesList" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="grdCategoriesList" runat="server" CssClass = "table table-hover table-responsive" Width="100%"
DataSourceID="odsCategoriesList" AllowPaging="true" PageSize="10" GridLines="None" BorderStyle="NotSet" BorderWidth="0px"
AutoGenerateColumns="false">
<PagerSettings FirstPageText="<< First" LastPageText="Last >>" NextPageText="Next >"
Position="Bottom" PreviousPageText="< Previous" />
<Columns>
<asp:TemplateField HeaderStyle-CssClass="input-group-addon" ItemStyle-CssClass="input-group-addon">
<ItemTemplate>
<asp:CheckBox ID="chkSelectItem" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server" />
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Category Name" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:LinkButton ID="lnkCategoryName" runat="server" OnCommand="lnkCategoryName_Command"
CommandArgument='<%# Eval("CategoryId") %>' CommandName='<%# Eval("CategoryName") %>'
Text='<%# Eval("CategoryName") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description" HeaderText="Description" ItemStyle-HorizontalAlign="Left" />
</Columns>
<PagerStyle HorizontalAlign="Right" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:ObjectDataSource ID="odsCategoriesList" runat="server" EnablePaging="True" TypeName="ProgrammersToolkitLibrary.Controller.CategoryController"
SelectMethod="GetListOfCategories" SelectCountMethod="GetListOfCategoriesCount"></asp:ObjectDataSource>
</div>
<div class="modal fade" id="AddCategoryModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Add New Category</h4>
</div>
<div class="modal-body">
<table style="width:100%;text-align:left;">
<tr>
<td style = "width: 30%;"><label for="txtCategoryName">Category Name:</label></td>
<td>
<asp:TextBox ID="txtCategoryName" runat="server" CssClass="form-control"></asp:TextBox>
</td>
</tr>
<tr>
<td style = "width:30%; vertical-align:text-top;"><label for="txtDescription">Description:</label></td>
<td>
<asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine" Rows="3" CssClass="form-control"></asp:TextBox>
</td>
</tr>
</table>
</div>
<div class="modal-footer">
<asp:LinkButton ID="lnkSave" runat="server" CssClass="btn btn-primary" OnClick="lnkSave_Click" ClientIDMode="Static">Save</asp:LinkButton>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</asp:Content>
Thank you.
Ju-chan

Categories