change div into html server control and add an click event - asp.net

I have a piece of code
<div class="class1">
<span>testing</span>
</div>
I will change this into html server control.
<div class="class1" id="div1" runat="server">
<span>testing</span>
</div>
I want to add a click event.
<div class="class1" id="div1" runat="server" onclick="Test_Click">
<span>testing</span>
</div>
On code behind, I have that event handler.
protected void Test_Click(object sernder, EventArgs e)
{
//code
}
But it is not working.. I tried by changing onclick to onserverclick. it is still not working..
Any other way?

From your post I got that you want to simple go to the server side event while clicking the Div.
You can't directly give this to a DIV, instead you can use a Hidden Button in the div and trigger the click event manually.
<script>
function clickDiv() {
if (__doPostBack) {
__doPostBack('<%=btnNew.UniqueID %>', '');
}
else {
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = '<%=btnNew.UniqueID %>';
theForm.__EVENTARGUMENT.value = '';
theForm.submit();
}
}
}
</script>
<div class="class1" id="div1" runat="server" onclick="clickDiv()">
<span>testing</span>
<asp:Button runat="server" Style="display: none" ID="btnNew" Text="Submit" OnClick="btnNew_Click" />
</div>
In _Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnNew_Click(object sender, EventArgs e)
{
result.Text += "hey, event handler! ";
}
}
You can see the jQuery Implementation over here:
JQuery UI Dialog & Asp (web forms) calling event handler

Related

how to call a back method from front asp.net

I'm trying to call a method written in the back from a div in the front
front:
<div runat="server" id="headerLink" onclick="headerLinkOnClick" style="cursor: pointer;">
<header>
</header>
</div>
back:
protected void headerLinkOnClick(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Roles.DeleteCookie();
Session.Clear();
Response.Redirect("~/default.aspx");
}
I must miss something but no idea what.
You can achieve this with a hidden button
<asp:Button runat="server" id="btnPostback" style="display:none" onclick="headerLinkOnClick" />
<div onclick="document.getElementById('<%= btnPostback.ClientID %>').click()">Clickable DIV</div>

asp.net page not scrolling

Here is my problem:
<asp:Panel ID="pnlMyAddressBook" runat="server" BackColor="White" CssClass="roundcorner">
TEST<table style="width: 100%;">
Do I need to explain that the page doesn't scroll to show the list of all the validation errors?
Its a panel being shown on a modal popup... I have tried setting the height property for the panel... no use...
Also, including this (required) Jquery function in the HTML causes the panel to move to a corner... I have removed it for now from my HTML...
<script type="text/javascript">
$(function () {
$("#tabs").tabs();
});
C# code for the button opening the modal:
protected void btnEdit_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "key", "launchModalV2();", true);
//ClearTextBoxes();
PopUpAddressInLightBox();
mpeTest.Show();
hdnfld.Value = "Edit";
}
HTML
<asp:Button ID="btnEdit" runat="server" Text="Edit Address pb" OnClick="btnEdit_Click" CssClass="btn" />
<asp:Button ID="btnAddNew" runat="server" Text="Add New Address pb" OnClick="btnAddNew_Click" />
I am using these 2 buttons to call the modal....
.modalBackground
{
background-color: Black;
-filter: alpha(opacity=80);
-opacity: 0.6;
z-index: 10000;
}
In the css of your modal popup, set the width of the modalpop up, then set the overflow:auto. That will give you vertical scrollbar. Example:
.ModalPopupPanel
{
height:700px;
overflow:auto;
}
So,when the content height exceed the 700px, the horizontal scrollbar will show up. The same is true for the horizontal scrollbar where you need to set the width of the modalpop.

radiobuttons to the left of label using CSS

I have been attempting to move the labels of my radiobuttons to the left with the help of CSS but nothing seems to work. Could i get a second pair of eyes to help me with this?
asp:RadioButtonList ID="rbid" runat="server" RepeatDirection="Vertical" RepeatLayout="Table" TextAlign="left" >
<asp:ListItem Value="a">a</asp:ListItem>
<asp:ListItem Value="b">b</asp:ListItem>
<asp:ListItem Value="c">c</asp:ListItem>
<asp:ListItem Value="d">d</asp:ListItem>
</asp:RadioButtonList>
css:
.FormArea input[type=radio]
{
float:left;
}
.FormArea input[type=radio] label
{
float:right;
}
In your radio button list you've set TextAlign="Left" but this get's overridden by
.FormArea input[type=radio] label
{
float:right;
}
So no surprise that it's not working.
You have two options:
Remove both css style rules and simply use TextAlign="Left" in the radio button list or
Remove TextAlign="Left" from the radio button list and use the following CSS:
.FormArea input[type=radio] { float: right; }
.FormArea input[type=radio] label { float: left;}
Both approaches will display the text to the left of the radio button as required:
Please mark as correct if the answer helps you
you need TextAlign = TextAlign.Left e.g. :
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
RadioButtonList1.TextAlign = TextAlign.Left;
}
protected void Button2_Click(object sender, System.EventArgs e)
{
RadioButtonList1.TextAlign = TextAlign.Right;
}
</script>
at the top of your page
look at this link

How to use different style for the first item in repeater?

For first item I want to use the following <div>:
<div class="six columns">
<a href="">
<img src="" />
<h3>Israeli Embassy Promotes Peace</h3>
<h4>At a time when Israel is facing threats of...</h4>
</a>
</div>
And This for rest i want to use following <div>:
<div class="six columns">
<ul>
<li><a href="">
<h3>This is the first alskdjlak s</h3>
</a></li>
<li><a href="">
<h3>asd sad asd asd asdasdasdas d</h3>
</a></li>
<li><a href="">
<h3>dsad asd asd asd asd asd asd</h3>
</a></li>
</ul>
</div>
How do i do it?
You first use one integer on the code behind, that is start with 1.
Then on repeater you check this value and you go like:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<% if (iCounter == 1) { %>
<br />First line id: <%# GetID(Container.DataItem) %>
<% } else { %>
<br />Next lines id: <%# GetID(Container.DataItem) %>
<% }
iCounter++;
%>
</ItemTemplate>
</asp:Repeater>
and on code behind:
public int iCounter = 1;
List<int> oMainIds = new List<int>();
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
oMainIds.Add(i);
}
Repeater1.DataSource = oMainIds;
Repeater1.DataBind();
}
public int GetID(object oItem)
{
return (int)oItem;
}
Please note, for the test, here I place and 10 data lines. This example renders:
First line id: 0
Next lines id: 1
Next lines id: 2
Next lines id: 3
Next lines id: 4
Next lines id: 5
Next lines id: 6
Next lines id: 7
Next lines id: 8
Next lines id: 9
Just an update to Aristos answer, without the code-behind method to get the ID.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Panel ID="pnlFirst" runat="server" Visible="<%# (Repeater1.DataSource as List<int>).IndexOf((int)Container.DataItem) ==0 %>">
<br />First Item
</asp:Panel>
<asp:Panel ID="pnlRest" runat="server" Visible="<%# (Repeater1.DataSource as List<int>).IndexOf((int)Container.DataItem) !=0 %>">
<br />
Item: <%# (Repeater1.DataSource as List<int>).IndexOf((int)Container.DataItem) %>
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
and in code behind
List<int> oMainIds = new List<int>();
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
oMainIds.Add(i);
}
Repeater1.DataSource = oMainIds;
Repeater1.DataBind();
}
Wrap your divs in PlaceHolder controls, and set the visibility of the PlaceHolders in the OnItemCreated event of the repeater using e.Item.ItemIndex to determine if the current item is the first one or not.
Or make them server-side divs (with runat=server) and set the visibility of the divs directly in the sme way
Use CSS.
.six_columns ul li
{
/* Item style */
}
.six_columns ul li:first-child
{
/* First item style */
}
There are also methods for editing content via CSS.

How to create loading image on page redirect on the time in asp.net

I need to show the loading image when I am going to click the button and redirect to the next page in asp.net.....thanks in advance
You can use a JavaScript to show the loading effect if your page is taking too much time or else you can set the timings to show a loading effect. Nevertheless JavaScript is a good option. You can find some very good JavaScripts on the Net that will be helpful to you.
Ex.
Try the below code:
In css class:
.divModalBackground
{
filter: alpha(opacity=50); -moz-opacity:0.5; opacity: 0.5;
width:100%;
background-color: #999988;
position: absolute;
top: 0px;
left: 0px;
z-index: 800;
}
In aspx page:
<asp:Panel ID="Panel1" runat="server" Height="900px" Width="100%"
CssClass="divModalBackground" Visible="true" >
<asp:Image runat="Server" ID="ImageLoader" CssClass="LoadingProgress"
ImageUrl="../../App_Themes/Black/images/ajax-loader.gif" />
</asp:Panel>
In aspx.cs page I put the code like this:
//to show modal popup at page startup
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Panel1.Visible = true;
}
In aspx page place this
// to clear the modal background when page load complete, place this code at the end of your page just before the end of the form tag
<script type="text/javascript" >
function init()
{
var objdiv=document.getElementById('<%=Panel1.ClientID%>')
if(objdiv)
{
objdiv.style.visibility = 'hidden';
}
}
init();
</script>
Use following code
$(function() {
($.unblockUI);
$('#pnlLogin_btnOk').click(function() {
$.blockUI({ message: '<img src="Images/loading.gif" />' });
});
});

Resources