Data List itemCommand function is not working - c#

I've tried to create a datalist with itemcommand function altough it seems that the program doesn't get into the function of the itemcommand for some reason.
aspx
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" DataKeyField="jobID">
<ItemTemplate>
<div class="jobContainer">
<div class="jobDetails">
<span class="jobName"><%# Eval("jobName") %></span><br />
<hr class="style13">
<a class="Details"> <b>Requirments: </b><span ><%# Eval("jobRequirments") %> WPM</span> </a>
<a class="Details"> <b>Salary: </b><span ><%# Eval("jobSalary")%> Shekel per hour</span> </a>
<a class="Details"> <b>City: </b><span ><%# Eval("jobCity")%></span>
<asp:Button ID="Button1" runat="server" CommandName="Details" Text="Show Details" />
</div>
</div>
</ItemTemplate>
</asp:DataList>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
// getjobs returns a dataset
Service.Service a = new Service.Service();
DataList1.DataSource = a.getjobs();
DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Details")
{
Response.Redirect("Home.aspx");
}
}

I suspect you are having an issue with Page.IsPostBack, so make sure that you bind your DataList when Page is not PostBack in Page_Load as following:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Bind your DataList
Service.Service a = new Service.Service();
DataList1.DataSource = a.getjobs();
DataList1.DataBind();
}
}
See more info about Page.IsPostBack

Related

Get cell value when click button in listview

I create a listview, and put button in every row of a table cell. in button click event , how to get the value in table , like Aitline, ArrCity or the index of items.
// in aspx
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<table class="table table-condensed table-striped table-hover">
<th><%#Eval("Airline")%></th>
<th><%#Eval("DepCity")%></th>
<th><%#Eval("DepTime")%></th>
<th><%#Eval("FlyTime")%> 分鐘</th>
<th><%#Eval("ArrTime")%></th>
<th><%#Eval("ArrCity")%></th>
<th class="success"><%#Eval("TotalFare")%></th>
<th><asp:Button type="submit" class="btn btn-primary btn-lg" id="PayButton" runat="server" Text="Pay" OnClick="PayButton_OnClick"/></th>
</table>
</li>
</ItemTemplate>
<EmptyDataTemplate>
<p>Nothing here.</p>
</EmptyDataTemplate>
</asp:ListView>
//in C# click event in PayButton
protected void Page_Load(object sender, EventArgs e)
{
List<Ticket> TicketList = new List<Ticket>();
this.ListView1.DataSource = TicketList;
this.ListView1.DataBind();
}
protected void PayButton_OnClick(object sender, EventArgs e)
{
// how to get table cell value here?
}
You can use the DataKeyNames attribute. For example:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID, Name" >
and get the value this way in code-behind:
protected void PayButton_OnClick(object sender, EventArgs e)
{
ListViewItem item = (sender as LinkButton).NamingContainer as ListViewItem;
int id = (int)ListView1.DataKeys[item.DataItemIndex].Values["ID"];
string name = (string)ListView1.DataKeys[item.DataItemIndex].Values["Name"];
}

i wanted to take a id of label from repeater?

I have a Repeater binded with database data. I need to find out the Product ID binded to Label but am unable to fetch it.
Here is my Aspx page
<asp:Repeater ID="rpProducts" runat="server" OnItemCommand ="add_click" >
<ItemTemplate>
<div style="visibility: hidden">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id")%>' ></asp:Label>
</div>
<div class="col-sm-4 prdcts">
<h3>
<%# Eval("productName")%></h3>
<div class="col-sm-12 prdctbox">
<span class="AddToCrt">
<div title="Add to Cart">
<%-- <em class="fa fa-plus"></em>--%>
<asp:ImageButton ID="ImageButton1" runat="server" onclick="add_click" Height="22px"
ImageUrl="~/static/uploads/images/1_1-128.png" Width="24px"/>
</div>
</span>
<div class="imgs">
<%# Eval("productDescription")%>
</div>
<%# Eval("listingHTML")%>
<div class="row">
More
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
my codding in c# is
//void rpProducts(object sender, RepeaterItemEventArgs e)
//{
// Label l = (Label)e.Item.FindControl("Label1");
// string s = l.Text;
//}
//protected void add_Click(object sender, RepeaterCommandEventArgs e)
//{
// Label l = (Label)e.Item.FindControl("Label1");
// string s = l.Text;
//}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
Label l = (Label)e.Item.FindControl("Label1");
string s = l.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
}
Is there any way to fetch the label value?
You don't need the hidden label at all. You just need to change markup for your button a little bit.
<asp:ImageButton ID="ImageButton1" runat="server" CommandArgument='<%# Eval("id")%>' ... />
And in you codebehind you can reference the id like:
protected void Button1_Click(object sender, EventArgs e)
{
var button = (IButton)sender;
// assuming id is Int32
int id = int.Parse(button.CommandArgument);
}
Your Label1 is inside ItemTemplate so correct way to fetch the control value is
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item|| e.Item.ItemType == ListItemType.AlternatingItem)
{
Label l = (Label)e.Item.FindControl("Label1");
string s = l.Text;
}
}

Disable button on update progress in ASP.net Ajax

Hi i am loading huge data by asp.net Ajax on button click in grid view showing loading message on update prgress ...in update progress time i have to disable my BtnLoadReport Button.
<td>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:UpdateProgress ID="updProgress"
AssociatedUpdatePanelID="UpdatePanel1"
runat="server" oninit="updProgress_Init" onprerender="updProgress_PreRender">
<ProgressTemplate>
<span style="color:green; font-style:italic;">Loading, please wait...</span>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<div id="DemoId">
<asp:Button ID="BtnLoadReport" runat="server" onclick="BtnLoadReport_Click"
Text="LoadReport" Width="176px" ClientIDMode="Static" OnClientClick="return clickOnLoadReport();" />
</div>
<asp:GridView ID="GridView1" runat="server" Height="170px"
onselectedindexchanged="GridView1_SelectedIndexChanged" Width="438px">
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="BtnLoadReport" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
my script to disable button
<script type="text/javascript">
var updateProgress = null;
function clickOnLoadReport() {
// This will disable all the children of the div
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm.get_isInAsyncPostBack()==false) {
var nodes = document.getElementById("DemoId").getElementsByTagName('*');
for (var i = 0; i < nodes.length; i++) {
nodes[i].disabled = true;
// nodes[i].off("click");
}
}
}
</script>
but it is not disabling my button for long it is disabling it for just seconds
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(this.BtnLoadReport);
}
protected void BtnLoadReport_Click(object sender, EventArgs e)
{
// System.Threading.Thread.Sleep(3000);
UpdatePanel1.Update();
DataSet dataSet = new DataSet();
dataSet.ReadXml(#"C\Data.Xml");
GridView1.DataSource = dataSet.Tables[0];
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void updProgress_Init(object sender, EventArgs e)
{
BtnLoadReport.Enable = false;
}
protected void updProgress_PreRender(object sender, EventArgs e)
{
BtnLoadReport.Enable
= true;
}
this above thing i tried to disable my button BtnLoadReport on Update progress still not working
Thanks in Advance
function clickOnLoadReport() {
var requestManager = Sys.WebForms.PageRequestManager.getInstance();
requestManager.add_initializeRequest(CancelPostbackForSubsequentSubmitClicks);
function CancelPostbackForSubsequentSubmitClicks(sender, args) {
if (requestManager.get_isInAsyncPostBack() &
args.get_postBackElement().id == 'BtnLoadReport') {
args.set_cancel(true);
document.getElementById("BtnLoadReport").setAttribute("disabled", "disabled");
//alert('A previous request is still in progress that was issued on clicking ' + args.get_postBackElement().id);
}
}
}
i made changes in my javascript function it solve my problem

How to get updated Textbox value from Repeater?

I have a repeater control as listed below. It has a textbox control. When a save button is clicked, I need to get the updated text from the textbox. I have the following code; but it gives me the old value when I take the textbox text.
How can we get the updated text?
Code Behind
protected void Save_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in repReports.Items )
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem )
{
string updatedEmail = ((TextBox)item.Controls[5]).Text;
string originalEmail = ((HiddenField)item.Controls[7]).Value;
}
}
}
Control Markup
<div class="repeaterTableBorder">
<asp:Repeater ID="repReports" runat="server">
<ItemTemplate>
<div id="repeaterIdentifier" class="repeaterIdentifier">
<div class="reportTitle">
<%# Eval("ReportName") + ":"%>
<asp:HiddenField ID="hdnLastChangeTime" runat="server" Value= '<%# ((DateTime)Eval("RecordSelectionTime")).ToString("MM/dd/yyyy hh:mm:ss.fff tt")%>' />
<asp:HiddenField ID="hdnReportID" runat="server" Value='<%# Eval("ReportTypeCode")%>' />
</div>
<div class="reportFrequency">
<%# " Frequency - Weekly" %>
</div>
</div>
<div class="reportContent">
<div class="repeaterLine">
<asp:TextBox ID="txtEmailRecipients" runat="server" class="textEdit"
Text='<%# Eval("ExistingRecipients") %>'
TextMode="MultiLine"></asp:TextBox>
<asp:HiddenField ID="hdnOriginalRecipients" runat="server" Value='<%# Eval("ExistingRecipients")%>' />
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
I assume that you are binding the Repeater to it's DataSource also on postbacks. You should do that only if(!IsPostBack). Otherwise the values will be overwritten.
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
// databinding code here
}
}

Running a C# and a Javascript function on an asp button control?

I can get both the Javascript and the C# function to work just fine.
However, my Javascript function runs before the C#.
How do I get it to run after the C# function??
Here is my code:
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
<div id="div2" style="height:70px; width:auto; text-align:center;">
<p><b>This is A View!!!</b></p>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<div id="div1">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"
OnClientClick="javascript:Highlit()" />
</div>
</asp:View>
</asp:MultiView>
<script type="text/javascript">
function Highlit()
{
$("#div2").effect("highlight", {}, 10000);
}
</script>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
namespace jQuery_Highlight.jQuery_Highlight
{
public partial class jQuery_HighlightUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Changed";
}
}
}
Here is the code reflecting changes from the answers:
Code behind
namespace jQuery_Highlight.jQuery_Highlight
{
public partial class jQuery_HighlightUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Changed";
ScriptManager.RegisterStartupScript(this, this.GetType(), "TEST", "Highlit();", true);
}
}
}
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
<div id="div2" style="height:70px; width:auto; text-align:center;">
<p><b>This is A View!!!</b></p>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<div id="div1">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</asp:View>
</asp:MultiView>
</ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript">
function Highlit() {
$("#div2").effect("highlight", { color: "#9499FC" }, 10000);
}
</script>
The only way to get the javascript to run after is to add a script reference in your Button1_Click event.
Example Code for standard postback:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Changed";
Page.ClientScript.RegisterStartupScript(this.GetType(), "PostButton1_ClickScript", "Highlit();", true);
}
As noted by others, be sure to remove your OnClientClick event. Also, consider moving your "Highlit" script outside of the update panel.
Additionally, since you are in an update panel, you will need to use the following example code for a Partial Postback:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Changed";
ScriptManager.RegisterStartupScript(this, this.GetType(), "PostButton1_ClickScript", "Highlit();", true);
}
You have to register a ClientScript at the end of Button1_Click event
and remove OnClientClick="javascript:Highlit()"
protected void Button1_Click(object sender, EventArgs e)
{
//Do stuff
ScriptManager.RegisterStartupScript(this, this.GetType(), "ANYNAME", "javascript:Highlit();", true);
}
Remove the OnClientClick attribute, and add the call as a startup script, so that it runs after the page has loaded:
protected void Button1_Click(object sender, EventArgs e) {
Label1.Text = "Changed";
Page.ClientScript.RegisterStartupScript(this.GetType(), "start", "Highlit();", true);
}
Side note: When you use the OnClientClick attribute, the code should not start with javascript:. That's only used when you put script in a href attribute in a link.

Categories

Resources