How to receive datalist item when sender is html button? - c#

In my DataList I want a button to be displayed with a FA icon, so I used a html button and made it runat="server", now when I click the button I want to know which Datalist Item is 'bounded' with this button.
I tried to use a asp.net button, but I can't use FA icons then.
This is what my html and c# code looks like:
<asp:DataList Width="100%" ID="dtlFAQSections" runat="server" DataSourceID="dtsFAQSections" DataKeyField="FAQSectionID">
<ItemTemplate>
<h2>
<button id="btnFAQSection" runat="server" onserverclick="btnFAQSection_Click" style="background-color:#f24646; border:none; color:white; margin-left:25px; font-size:16px; cursor:pointer; height: 26px; width: 26px; margin-right: 5px; border-radius:30%;"><i class="fas fa-plus"></i></button>
<asp:Label Font-Size="18px" ID="FAQSectionNameLabel" runat="server" Text='<%# Eval("FAQSectionName") %>' />
</h2>
<hr style="border: 1px dotted #000000; border-style: none none dotted; color: #fff; background-color: #fff;"/>
</ItemTemplate>
</asp:DataList>
protected void btnFAQSection_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
DataListItem item = (DataListItem)btn.NamingContainer;
}

First, you need to use HtmlButton since you are not using an ASP control as a Button. Then simply find the Parent.
protected void btnFAQSection_Click(object sender, EventArgs e)
{
HtmlButton btn = (HtmlButton)sender;
DataListItem item = (DataListItem)btn.NamingContainer;
//now you can access the DataListItem
Label label = item.FindControl("FAQSectionNameLabel") as Label;
label.Text = "DataListItem Found";
//or if you want to get the parent DataList
DataList dl = btn.Parent.Parent as DataList;
Label1.Text = dl.ID;
}

Related

How do I refresh a web controler inside a gridview ASP.NET

How can I refresh a web controller inside a gridview? I have a web controller inside a gridview as you can see here:
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%" style="background: #F5F5F5">
<div id="div<%# Eval("contrato_id") %>" style="overflow: auto; display: none; position: relative; left: 15px; overflow: auto">
<div class="ExpandTableHeader">
</div>
<div class="body">
<div id="tabs-ComponentesSection" class="menusection">
<TWebControl5:WebControl5 ID="Header8" runat="server" />
</div>
</ItemTemplate>
</asp:TemplateField>
I want to refresh the following controller.
<TWebControl5:WebControl5 ID="Header8" runat="server" />
On the following button click.
<asp:Button class="btn btn-primary" ID="btnChooseContract" runat="server" Text="Elegir contrato" OnClick="AddContractToQuote" />
Here is the code behind of the button.
protected void AddContractToQuote(object sender, EventArgs e)
{
}
Used some code that looks like this YEARS ago...
for (int x = 0; x < GridViewName.Rows.Count; x++)
{
GridViewRow row = (GridViewRow) GridViewName.Rows[x];
Control contr = (control) row.Cells[1].FindControl("NameOfYourControl");
contr = //Another similar control that looks like the first, but different
}

button event accidentally occured after textchanged

I have a problem with textchanged event on textbox.
After i input some string and hit enter, i don't know why button click is fired too
here my .aspx
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="labelKodeDivisi" runat="server" style="z-index: 1; left: 62px; top: 57px; position: absolute" Text="labelKodeDivisi"></asp:Label>
<asp:Label ID="labelNamaDivisi" runat="server" style="z-index: 1; left: 62px; top: 85px; position: absolute" Text="labelNamaDivisi"></asp:Label>
<asp:Button ID="butLogout" runat="server" OnClick="butLogout_Click" style="z-index: 1; left: 682px; top: 40px; position: absolute" Text="Button" />
<asp:TextBox ID="tbKode" runat="server" OnTextChanged="tbKode_TextChanged" style="z-index: 1; left: 71px; top: 127px; position: absolute"></asp:TextBox>
</form>
and here my aspx.cs
public partial class FormDivisi2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void butLogout_Click(object sender, EventArgs e)
{
labelKodeDivisi.Text = "123";
}
protected void tbKode_TextChanged(object sender, EventArgs e)
{
labelNamaDivisi.Text = "555";
}
}
Whats going on is, when i input text in textbox tbKode and hit enter, labelNamaDivisi.text change to 555 but labelKodeDivisi.text also change to 555.
What i want is when i input some text in textbox tbKode, labelNamaDivisi.text change to 555 but labelKodeDivisi not change. labelKodeDivisi only change when i hit button butLogout
you can try this
$('input').keypress(function(event){
if(event.keyCode==13)
{
event.preventDefault();
}
})

How to display DIV popup on Gridview row click event in asp.net

I am using a web page which contains a grid view with the list of datas in that. I want to display the selected grid view row details into a popup or dialogue window on row click event.For that I have created one DIV and design it with proper control values. But I am not able to display the DIV on row click event.
Here is my Source code:
<div id="divPopup" runat="server" style="border:1px solid black;display:none ">
<fieldset>
<legend>Disabling User Account</legend>
<table style="height: 112px; font-weight: 700; font-size: medium; font-family: Cambria; color: #000000; width: 316px">
<tr>
<td>
<asp:Label ID="lblGlobalId1" runat="server" Text="Global ID" />
</td>
<td>
</td>
<td>
<asp:Label ID="lblGlobalIdValue1" runat="server" Text="80007929" />
</td>
</tr>
.......
.......
<asp:Button ID="btnContinue1" runat="server" Height="26px" style="font-weight: 700"
Text="Continue" Width="92px" />
</table>
</fieldset>
</div>
.aspx Code is,
protected void grdADActionList_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (GridViewRow gvr in grdADActionList.Rows)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.background='#eeff00';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.background='#ffffff';";
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdADActionList, "Select$" + e.Row.RowIndex);
}
}
protected void grdADActionList_SelectedIndexChanged(object sender, EventArgs e)
{
//lblGlobalIDValue.Text = "278900078";
//lblGivenNameValue.Text = "Surya";
//lblEmailValue.Text = "surya#abc.com";
divPopup.Visible = true;
}
On page load event I am disabling that DIV visibility and on SelectedIndexChanged event I am enabling the DIV control visibility but the DIV control could not appear on the page. Can you please let me know How can i display the DIV popup control on my page? also I want to display it on the page center.
Please suggest me about the problem and tell me the solution for the same.
Thanks.

cant access child control with FindControl()

All of this happens within the same user control so that shouldnt make a difference.
<asp:Repeater ID="rptActivity" runat="server" OnItemCreated="rptActivity_ItemCreated">
<ItemTemplate>
<div class="under-label">
<div class="activity">
<%#Eval("ActivityName")%>
<input type="hidden" name="activityId" value='<%#Eval("ActivityId")%>' />
</div>
<div class="status">
<asp:DropDownList ID="ddlStatuses" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Id" runat="server"></asp:DropDownList>
</div>
<div class="comment">
<textarea name="comments" cols="35" rows="3" name="comment" style="float: left; margin: 0px 0px 0px 25px; font-family: Geneva, Arial, Helvetica, sans-serif;"><%#Eval("Comment")%></textarea>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
The i have the following code in the repeater's itemcreated event:
protected void rptActivity_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var helper = (UpdateActivitiesHelper)e.Item.DataItem;
DropDownList ddl = (DropDownList)FindControl("ddlStatuses");
ddl.SelectedValue = helper.StatusId.ToString();
}
and when i try to use ddl it throws NullReferenceException.
Any ideas?
Since your drop down list is inside the repeater, make sure you reference the DataItem to find the control.
Make sure to use e.Item.FindControl rather than Page.FindControl -- Page.FindControl will not find this item because it will not recursively search the page
protected void rptActivity_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var helper = (UpdateActivitiesHelper)e.Item.DataItem;
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatuses");
ddl.SelectedValue = helper.StatusId.ToString();
}
Try to modify your ItemCreated eventHandler like below and see if it works.
protected void rptActivity_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item){
var helper = (UpdateActivitiesHelper)e.Item.DataItem;
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlStatuses");
ddl.SelectedValue = helper.StatusId.ToString();
}
}

Checking username availability

I have created an check username availability inside a createuserwizard. And i had added a progress indicator to the checking process and would show a spinner image (in gif format) and it was done by using java script.
if the system is in the midst of checking the username in database, it would display the spinner image and at the same time display a text "Checking availability..."
And the problem not is the spinner image do not appear when it was checking..
Here is code:
<script language="javascript" type="text/javascript">
// Hook the InitializeRequest event.
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequest);
function InitializeRequest(sender, args) {
// Change div's CSS class and text content.
$get('UserAvailability').className = 'progress';
$get('UserAvailability').innerHTML = 'Checking availability...';
}
</script>
<asp:UpdatePanel runat="server" ID="up1">
<ContentTemplate>
<tr>
<td class="style4">Username:</td>
<td>
<asp:TextBox runat="server" ID="UserName" AutoPostBack="True"
ontextchanged="Username_Changed" Width="190" />
<div runat="server" id="UserAvailability" style="background-position: left; background-repeat: no-repeat; margin-left: -250px; padding-left: 22px; float:right;"></div>
</td>
</tr>
</ContentTemplate>
</asp:UpdatePanel>
Here is the code behind:
protected void Username_Changed(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(2000);
TextBox UserNameTextBox = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName");
if (Membership.GetUser(UserName.Text) != null)
{
UserAvailability.InnerText = "Username taken, sorry.";
UserAvailability.Attributes.Add("class", "taken");
}
else
{
UserAvailability.InnerText = "Username available!";
UserAvailability.Attributes.Add("class", "available");
}
}
I have used a masterpage, I had tried putting the JS file inside masterpage, but the image still not appearing.
EDIT
<style type="text/css">
#UserAvailability
{
padding-left: 22px;
margin-left: 30px;
float: left;
background-position: left;
background-repeat: no-repeat;
}
.progress
{
background-image: url(Images/spinner.gif);
}
.taken
{
background-image: url(Images/taken.gif);
}
.available
{
background-image: url(Images/available.gif);
}
</style>

Categories