asp:listbox loosing value on auto postback - asp.net

I have an asp list box that is populated from the code behind
<asp:ListBox ID="UploadsApprovedCountries" EnableViewState="true" runat="server" Rows="1" AutoPostBack="True" OnSelectedIndexChanged="SelectedCountryChange"></asp:ListBox>
protected void Load_Countries()
{
ListItem ps = new ListItem("Please select", "");
ps.Value = "";
UploadsApprovedCountries.Items.Add(ps);
foreach (string cntry in Countries.CountriesArray())
{
UploadsApprovedCountries.Items.Add(new ListItem(cntry, cntry));
}
}
and in my Page_Load I call it like this
if (!IsPostBack)
{
Load_Countries();
}
and on autopostback calls this method
protected void SelectedCountryChange(object sender, EventArgs e)
{
filter = UploadsApprovedCountries.SelectedItem.Value;
Response.Write("line 589");
}
However nothing I seem to do ever actually calls this method. The filter string is never populated and "Line 589" is never wrote out.
Can anyone help out
the whole of my asp.net page
asp:Content ContentPlaceHolderID="ContentMainBody" ID="LocalMainBody" runat="server">
<h1> administration</h1>
<div class="clearboth"></div>
<div>
<div class="demo">
<div id="tabs">
<ul>
<li>Uploads awaiting approval</li>
<li>Uploads approved</li>
<li>Sign-ups awaiting approval</li>
<li>Sign-ups approved</li>
<li>Rejected</li>
</ul>
<div id="tabs-5">
<h2>Rejected submissions</h2>
<div id="rejectedSubmissions" runat="server">
</div>
</div>
<div id="tabs-1">
<h2>Uploads awaiting approval</h2>
<div id="matUnautherised" runat="server">
</div>
<div class="clearboth"></div>
</div>
<div id="tabs-3">
<h2>Uploads live on site</h2>
<asp:ListBox ID="UploadsApprovedCountries" EnableViewState="true" runat="server" Rows="1" AutoPostBack="True" OnSelectedIndexChanged="SelectedCountryChange" OnTextChanged="SelectedCountryChange"></asp:ListBox>
<div id="matAutherised" runat="server">
</div>
<div class="clearboth"></div>
</div>
<div id="tabs-2">
<h2>Sign-ups awaiting approval</h2>
<div id="signups" runat="server">
</div>
<div class="clearboth"></div>
</div>
<div id="tabs-4">
<h2>Sign-ups live on site</h2>
<div id="signupsapproved" runat="server">
</div>
</div>
</div>
</div>
</div>
I have just noticed that if I change my code in my listbox to this
<asp:ListBox ID="UploadsApprovedCountries" EnableViewState="true" runat="server" Rows="1" AutoPostBack="True" OnSelectedIndexChanged="SelectedCountryChange">
<asp:ListItem Value="test1" Text="test1"></asp:ListItem>
<asp:ListItem Value="test2" Text="test2"></asp:ListItem>
<asp:ListItem Value="test3" Text="test3"></asp:ListItem>
<asp:ListItem Value="test4" Text="test4"></asp:ListItem>
<asp:ListItem Value="test5" Text="test5"></asp:ListItem>
</asp:ListBox>
and don't populate it from the data source then it works as it should. Is there a difference between adding the items from the code behind and adding them directly like above?

simple really once you know how
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Load_Countries();
UploadsApprovedCountries.EnableViewState = true;
}

Related

Get value from input checkbox tag from bootstrap in .NET

I'm trying using checkbox from Bootstrap (devoops.me) with ASP.NET.
On page I have:
<div class="row">
<div class="col-lg-12">
<div class="checkbox">
<label>
<input type="checkbox" name="testme" />some text for checkbox to choose
<i class="fa fa-square-o"></i>
</label>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</div>
On server side:
protected void Button1_Click(object sender, EventArgs e)
{
HtmlInputCheckBox chk = (HtmlInputCheckBox)this.Form.FindControl("testme");
if (chk.Checked == true)
{
Label1.Text = "YES it's checked";
}
}
But all I get is null.
I don't want to use jQuery or JavaScript. Is is possible?
Could anybody help me ?
FindControl searches for the ASP.NET controls. In order to register the checkbox as an asp.net control, provide an id and add the runat="server" attribute.
<div class="row">
<div class="col-lg-12">
<div class="checkbox">
<label>
<input type="checkbox" id="testme" runat="server" />some text for checkbox to choose
<i class="fa fa-square-o"></i>
</label>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</div>
The server side code can be simplified as follows:
protected void Button1_Click(object sender, EventArgs e)
{
if (testme.Checked == true)
{
Label1.Text = "YES it's checked";
}
}
Since the checkbox is a registered control, it is directly accessible in the code behind.

Dropdownlist in Update Panel Populated on SelectedIndexChanged of Another does not Populate

I do not know what happened here as I know for a fact that this code was working. The only thing I know for sure that changed in the project was an upgrade to jQuery 3.31. I have 2 dropdownlists in an UpdatePanel in a modal. The first list populates as expected. The second dropdown is supposed to populated OnSelectedIndexChanged event of the first. The event is firing, and there are 5 records returned, but the dropdownlist is blank.
UPDATE After looking at previous versions in TFS this did occur after upgrading to jQuery 3.31.
Here is the modal code:
<div id="modalTicketReportSelect" class="md-modal colored-header custom-width md-effect-9 primary">
<div class="md-content">
<div class="modal-header">
<button type="button" data-dismiss="modal" aria-hidden="true" class="close md-close"><i class="icon s7-close"></i></button>
<h3 class="modal-title">Ticket Type Report</h3>
</div>
<div class="modal-body form">
<div class="panel panel-alt3 panel-transparent">
<div class="panel-heading panel-heading-cg">
<button runat="server" id="btnAddToGrid" type="button" class="btn btn-alt3" data-dismiss="modal" onserverclick="btnAddToGrid_ServerClick" ><i class="icon s7-check"></i> Save</button>
<button type="button" data-dismiss="modal" class="btn btn-alt3 md-close"><i class="icon s7-close"></i> Cancel</button>
</div>
</div>
<asp:UpdatePanel runat="server" UpdateMode="Always">
<ContentTemplate>
<div class="form-group">
<label class="text-danger">Ticket Type</label>
<asp:DropDownList ID="ddlTicketTypes" runat="server" CssClass="form-control" AutoPostBack="true" OnSelectedIndexChanged="ddlTicketTypes_SelectedIndexChanged"></asp:DropDownList>
</div>
<div class="form-group">
<label class="text-danger">Ticket Report</label>
<asp:DropDownList ID="ddlTicketReport" runat="server" CssClass="form-control" ></asp:DropDownList>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
</div>
Here is the event handler:
protected void ddlTicketTypes_SelectedIndexChanged(object sender, EventArgs e)
{
//log postback
LoggerBA.Log(DB_Context, Path.GetFileName(Request.ServerVariables["SCRIPT_NAME"]), MethodBase.GetCurrentMethod().Name, UtilityBA.LoggerLevel.Info, "postback");
ticketProfileId = Guid.Parse(ddlTicketTypes.SelectedValue);
ticketProfileName = ddlTicketTypes.SelectedItem.Text;
//PopulateTicketReports(ticketProfileId.ToString());
Guid TicketProfileId = Guid.Empty;
if (Guid.TryParse(ticketProfileId.ToString(), out TicketProfileId))
{
LookupListItemBO SReportTypeListItem = LookupListBA.LookupListItem_GetByBothDatakeys(DB_Context, "SYS_REPORTTYPE", "REPORTTYPE_SINGLE");
Guid ReportTypeId = SReportTypeListItem.LookupListItemId;
List<CG.Reports.Views.ReportView.SavedReportBO> singleticketreports = CG.Reports.BLL.DevExpressBA.SavedReport_GetByModuleReportTypeId_AndTypeIdentifierId(DB_Context, "TICKET", ReportTypeId, TicketProfileId, true);
ddlTicketReport.DataSource = singleticketreports;
ddlTicketReport.DataTextField = "ReportName";
ddlTicketReport.DataValueField = "SavedReportId";
ddlTicketReport.DataBind();
ListItem oSelect = new ListItem("[Make Selection]", "");
ddlTicketReport.Items.Insert(0, oSelect);
ddlTicketReport.SelectedIndex = 0;
}
else
{
ddlTicketReport.Items.Clear();
}
}
Dev tools reveal no errors on page. I am stumped! Any assistance is greatly appreciated.

How To change attribute in html elements in asp.net

this is my asp codes in my page
<asp:GridView BorderStyle="None" BorderColor="White" BorderWidth="0px" CellPadding="0" AlternatingRowStyle-BackColor="" ID="grdProducts" Width="100%" runat="server" AutoGenerateColumns="False" ShowHeader="False" AllowPaging="True" DataSourceID="sqldsProducts">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<!--begin col-lg-6 col-md-6 -->
<li class="list_item col-lg-12 col-md-12 portfolio">
<div class="recent-item">
<figure class="portfolio_1">
<div class="bwWrapper touching medium">
<asp:Image ID="Image1" ImageUrl='<%# Eval("PImg") %>' runat="server" />
</i>
</div>
<figcaption class="item-description">
<h5 id="PTitle"><%# Eval("PTitle") %></h5>
<p id="PDesc"><%# Eval("PDesc") %></p>
<div class="go_link">
Read More
</div>
</figcaption>
</figure>
</div>
</li>
<!--end col-lg-6 col-md-6 -->
<div style="height:20px;"></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<pagerstyle cssclass="pagination" HorizontalAlign="Center" />
</asp:GridView>
for example i want to add some css attributes for this code
h5 id="PTitle"<%# Eval("PTitle") %>/h5 but i dont know how to find this id and add attributes
You can do this using jQuery. Make sure you have jQuery embedded in your page.
Then you can access PTitle element using jQuery selector and assign CSS to it as follows
$( document ).ready(function() {
$("#PTitle").css('background-color','#ff0000');
});
EDIT:
If you have ASPX page
<h5 id="PTitle" runat="server">Test</h5>
Then Code Behind:
PTitle.Style.Add("display", "none");
Depends on when you want to add the Styles. Here are few options.
If you want to add it in design time just add a style attribute as you would for other html elements.
<h5 id="PTitle" style="font-size:12px"><%# Eval("PTitle") %></h5>
If you want to do it from code behind (server side) you need to add the runat="server" attribute to the element first. In this case you won't add the style attribute in design time.
<h5 id="PTitle" runat="server"><%# Eval("PTitle") %></h5>
Then you can access this control in code behind using it's id as follows.
for (int i = 0; i < grdProducts.Rows.Count; i++)
{
HtmlGenericControl PTitle = (HtmlGenericControl)grdProducts.Rows[i].FindControl("PTitle");
if (PTitle != null)
{
PTitle.Style.Add("font-size", "12px");
}
}
Or, you could use the Attributes property as follows.
for (int i = 0; i < grdProducts.Rows.Count; i++)
{
HtmlGenericControl PTitle = (HtmlGenericControl)grdProducts.Rows[i].FindControl("PTitle");
if (PTitle != null)
{
PTitle.Attributes.Add("style", "font-size:12px");
}
}
If you need to do this in client side make use you JavaScript/ JQuery.
Okay here's all I needed to solve my problem:
LiteralControl litc=new LiteralControl();
litc.Text = "<style type='text/css'> #PTitle{text-align:right} #PDesc{text-align:right} </style>";
this.Page.Header.Controls.Add(litc);

asp.net code behind does not redirect when using jquery mobile

I have a jquery mobile page that uses a dialog page to perform an action. When the buttons are clicked I execute the code in my code behind which works perfectly however, I need to redirect or close the dialog page which for some reason it is not working. Here is my code for the dialog page:
<form id="form1" runat="server">
<div>
<div data-role="header" role="banner">
<h1 role="heading">Claim Issue?</h1>
</div>
<div data-role="content" data-theme="c" class="ui-corner-bottom ui-content ui-body-c" role="main">
<div data-role="fieldcontain">
<label for="CompanyLBL">Company:</label>
<asp:Label ID="CompanLBL" runat="server"></asp:Label>
</div>
<div data-role="fieldcontain">
<label for="IssueLBL">Issue:</label>
<asp:Label ID="IssueLBL" runat="server" Text="Label" ></asp:Label>
</div>
<div data-role="fieldcontain">
<label for="FBVersionLBL">Version:</label>
<asp:Label ID="FBVersionLBL" runat="server"></asp:Label>
</div>
<div data-role="button">
<asp:Button ID="ClaimBTN" runat="server" Text="Yes" onclick="ClaimBTN_Click" />
</div>
<span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Cancel</span></span>
</div>
</div>
Code Behind:
//Claim Issue
protected void ClaimBTN_Click(object sender, EventArgs e)
{
try
{
Tier2IssuesSDS.UpdateCommand = "UPDATE Tier2Issues SET Tier2ID = #Tier2ID, IssueStatusID = #IssueStatusID, DateLastModified = #DateLastModified, UserLastModified = #UserLastModified WHERE ID = #ID";
Tier2IssuesSDS.UpdateParameters["ID"].DefaultValue = Request.QueryString["IssueID"].ToString();
Tier2IssuesSDS.UpdateParameters["Tier2ID"].DefaultValue = Session["UserID"].ToString();
Tier2IssuesSDS.UpdateParameters["IssueStatusID"].DefaultValue = "2";
Tier2IssuesSDS.UpdateParameters["DateLastModified"].DefaultValue = DateTime.Now.ToString();
Tier2IssuesSDS.UpdateParameters["UserLastModified"].DefaultValue = Session["Username"].ToString();
Tier2IssuesSDS.Update();
Response.Redirect("~/Account/Tier 2/Admin/Mobile/Queue.aspx");
}
catch(Exception ex)
{
Response.Redirect("~/Account/Tier 2/Admin/Mobile/Queue.aspx");
}
}
What am I missing or what is the work around to get this thing to close the dialog or successfully redirect the browser to the starting page?

How to keep child controls and only delete surrounding parent control?

I have a piece of code like this:
<div id="container" runat="server">
<div id="parent" runat="server">
<div id="child" runat="server">
<p>Some Content</p>
</div>
</div>
</div>
In a certain situation i want to remove the parent DIV and leave the child DIV intact.
Using something like this removes the complete html (parent + child):
container.Controls.Remove(container.FindControl("parent"))
or
parent.visible = false
Is it possible to keep the html within the parent DIV (child DIV) and removing the surrounding parent DIV?
Any help appreciated.
Marcellino
Try this
<div id="container" runat="server">
im container
<br />
<div id="parent" runat="server">
im parent
<br />
<div id="child" runat="server">
i am child
<br />
</div>
</div>
</div>
<asp:Button runat="server" Text="remove" OnClick="remove_clicked" />
<input type="button" value="client remove" onclick="remove();" />
1. For Server side solution
protected void remove_clicked(object sender, EventArgs e)
{
HtmlGenericControl tempChild = child;
container.Controls.Remove(parent);
container.Controls.Add(tempChild);
}
2. For Client side solution
<script type="text/javascript" language="javascript">
function remove() {
var container = document.getElementById('<%= container.ClientID %>');
var parent = document.getElementById('<%= parent.ClientID %>');
var child = document.getElementById('<%= child.ClientID %>');
container.removeChild(parent);
container.appendChild(child);
}
</script>

Resources