I am having the following problem: I want to call a method whenever a specific li is clicked. Problem is that the li is dynamically created in a literal.text string, where I do import things from my database.
Whenever I try to call a method it does not work. I want to call a method whenever the user clicks on each li and get that li information inside my method (haven't wrote the method code yet, because I can't get it called.)
Thoughts?
protected void Page_Load(object sender, EventArgs e)
{
string conString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source="
+ Server.MapPath("~/ebookstoredb.mdb");
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
string query = "SELECT * FROM CATEGORY";
using (OleDbCommand cmd = new OleDbCommand(query, con))
{
OleDbDataReader reader = cmd.ExecuteReader();
String msg = "";
while (reader.Read())
{
lit1.Text += "<ul>" + "<li runat=\"server\" OnClick=\"ProductsInfo\">" + reader["ID"]
+ "," + reader["Name"]
+ "</li>"
+ "</ul>";
}
reader.Close();
}
con.Close();
}
}
protected void ProductsInfo(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
Unfortunately, you cannot create server-side events on a Literal Control.
However, you can add client-side javascript functionality to post back a request.
In your aspx using:
<div>
<asp:Literal runat="server" ID="lit1"></asp:Literal>
</div>
Your aspx.cs should contain:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
lit1.Text += "<ul>" + "<li \' onclick=\'javascript: __doPostBack(\"getProduct\", \"1\");\'>"
+ "Product " + "1"
+ "</li>"
+ "</ul>";
}
if (Request.Form["__EVENTTARGET"] != null && Request.Form["__EVENTTARGET"] == "getProduct")
{
getProduct_Click(null, null);
}
}
private void getProduct_Click(object sender, System.EventArgs e)
{
Response.Write("You Clicked on " + Request.Form["__EVENTARGUMENT"]);
}
This will set up each li to pass their own value to the event argument hidden control and perform a postback to the server.
You can then check if the event target is the required one and call a method with the value that was posted back.
Just change the sample lit1 text above to iterate through your data.
I've never used literals before, but from reading about them... with a literal displaying just static html, you will need to have each li call a javascript function that will then do a postback to the method you want. So build your li like this:
lit1.Text += "<ul><li><a onclick=\"CallProductInfo(" + reader["ID"]+ ")\">" + reader["ID"]
+ "," + reader["Name"]
+ "</a></li></ul>";
Then you have to have a javascript function that does the actual postback to your server side code or redirects to the products info page with the passed id. That is, in your page (not the code behind), have a script something like this for the postback...
<script>
function CallProductInfo(id)
{
__doPostBack('ProductInfoId', id);
}
</script>
In you code behind, in your page load event handler, you'd have something like this:
if (Request["__EVENTTARGET"] == "ProductInfoId")
{
ProductInfo(Convert.ToInt64(Request["__EVENTARGUMENT"]));
}
Ok so guys i found out what was the problem. Our mr.genious instructor told us today that we can use a datagrid..Sigh.
Thanks everyone for your replies!
Related
private void timer1_Tick(object sender, EventArgs e)
{
try
{
this.Text = this.comboBox1.SelectedIndex.ToString() + "/" + (object)this.comboBox1.Items.Count;
string str = new WebClient().DownloadString("webpage.com" + this.comboBox1.SelectedItem.ToString());
if (!str.Contains("Message"))
this.listBox1.Items.Add((object)this.comboBox1.SelectedItem.ToString());
else if (str.ToLower().Contains(this.comboBox1.SelectedItem.ToString()))
;
++this.comboBox1.SelectedIndex;
}
catch
{
if (this.comboBox1.SelectedIndex != this.comboBox1.Items.Count - 1)
return;
this.timer1.Stop();
}
}
I want to check whether the url page contains a specific html class name in the page its on html code
If you really don't want to use HtmlAgilityPack then you could try something like this:
str.Contains("class=\"" + nameOfClass)
I would also recommend to remove whitespaces before comparison.
So finally.. I have my repeater working as I want it to be full of buttons, radio button, image buttons, update panels, AJAX modal popups and a heavy code behind each event.
found out that my repeater getting very slow when the items exceeds 20, so I used paging as a solution. the problem is when I do changes and move on to the next page, all changes are gone when getting back to the previous page. (checked radios, labels, etc all back to normal state).
please help, my system is in production now.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadTasks();
}
void loadTasks()
{
string evalidxxx = Request.QueryString["eval_id"].Trim().Replace(" ", "");
SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["GappConnectionString2"].ConnectionString);
try
{
conn.Open();
SqlDataAdapter sqlAdapter = new SqlDataAdapter("SELECT Prog_Task_link.pt_seq, Tasks.task_name, Tasks.task_id FROM Tasks INNER JOIN Prog_Task_link ON Tasks.task_id = Prog_Task_link.task_id INNER JOIN Programs ON Prog_Task_link.prog_id = programs.prog_id INNER JOIN Data_Tracker_prepare ON Programs.prog_id = Data_Tracker_prepare.dtpre_prog_id WHERE Data_Tracker_prepare.eval_id =" + evalidxxx, conn);
System.Data.DataTable dt = new System.Data.DataTable();
sqlAdapter.Fill(dt);
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = dt.DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 10;
objPds.CurrentPageIndex = CurrentPage;
lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of "
+ objPds.PageCount.ToString();
//Disable Prev or Next buttons if necessary
LinkPrevPage.Enabled = !objPds.IsFirstPage;
LinkNextPage.Enabled = !objPds.IsLastPage;
rptr1.DataSource = objPds;
rptr1.DataBind();
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally { conn.Close(); }
}
public int CurrentPage
{
get
{
// look for current page in ViewState
object o = this.ViewState["_CurrentPage"];
if (o == null)
return 0; // default to showing the first page
else
return (int)o;
}
set
{
this.ViewState["_CurrentPage"] = value;
}
}
protected void LinkPrevPage_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
loadTasks();
}
protected void LinkNextPage_Click(object sender, EventArgs e)
{
CurrentPage += 1;
loadTasks();
}
if you are using .net 4.0 you can use EnablePersistedSelection="True"
sorry to say but this can not be done..
When you are in page one, the rest pages are not actual exists in repeater.
after paging to next page next data will be load so previous data will be reset
i mean to say, When you ask to select all radios, buttons,check boxes are mean what you see. After you change page the other controls are set to default (probably unchecked or reset)
So re design your user interface for what you want to do. And as my suggestion take another button to save the current state of the page.. and then go to next page..
anything other that i can help ??
namespace KetBanBonPhuong.Controls.Default
{
public partial class SugFriends : System.Web.UI.UserControl
{
private string Uid;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["UId"] != null)
{
string value = Request.Cookies["UId"].Value;
Uid = UserService.GetId_Cookie(value);
}
else
{
Uid = Session["Id"].ToString();
}
LoadListSuggest();
}
private void LoadListSuggest()
{
string str = "";
List<RankByUser> list = new List<RankByUser>();
list = RankByUserService.GetListRank(Uid);
foreach (RankByUser rank in list)
{
str += "<li><div class=\"sug_acc\">"
+ "<img src=\"" + rank.Avatar + "\" alt=\"avatar\"/>"
+ "" + rank.LastName + " " + rank.FirstName + ""
+ "</div>"
+ "<div class=\"rank\">"
+ "rank: " + rank.Rank + ""
+ "Kết bạn"
+ "</div></li>";
}
ltrListSug.Text = str;
}
}
}`
It's a user control SugFriends.ascx being add in Default.Master
When I click "a.button" postback event to server?(I want to insert data to database, I used Sql server)
How to do it? Make tag a event onclick 'like' LinkButton: Onclick()?
Thanks for your helping! I found solution that problem! I used Ajax onclick for each tag a, event post Ajax.aspx, in here I can code work with database!
Add an onclick="(javascript:__doPostBack('','');" attribute to the <a> tag to perform a postback via Javascript. Like this:
+ "Kết bạn"
More details here
Response to comment:
Try creating a button on the page
<asp:Button ID="btnPlaceHolder" Visible="False" runat="server" /> and then do what I said in this solution but instead use __doPostBack('<%=btnPlaceHolder.UniqueID %>', '')
From there, you can use the method block
Private Sub btnPlaceHolder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlaceHolder.Click to run the code you want on postback.
I have a very strange issue where SelectionList always returns NULL when i try check its Selected Item/Value. I Googled a bit and I found out that when i click the submit button, the page is being refreshed and the SelectionList is being data bound again so it will revert back to its original behavior.
Then i tried enclosing the binding code in the Page_Load event in a !IsPostBack but still when i try to access the Selected property it is null and an exception is thrown.
Any help would be greatly appreciated.
My code goes something like this... (the braces are not matched properly)
static SelectionList[] Symptoms;
static string UserID = "";
cmbSymptoms1,cmbSymptoms2,cmbSymptoms3 and cmbSymptoms4 are SelectionLists. I took them in to an array of SelectionList and then set the properties.
I had to make them static or else when i click the button to update, they will not retain their values. Any idea why they don't retain the values?
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
//System.Diagnostics.Debug.WriteLine("Not IsPostBack");
if (Request.QueryString["id"] != null && Request.QueryString.ToString() != "")
{
//System.Diagnostics.Debug.WriteLine("id query string is not null :- " + Request.QueryString["id"]);
myclass = new Class1();
UserID = Request.QueryString["id"];
Symptoms = new SelectionList[4];
Symptoms[0] = cmbSymptoms1;
Symptoms[1] = cmbSymptoms2;
Symptoms[2] = cmbSymptoms3;
Symptoms[3] = cmbSymptoms4;
System.Data.DataTable dt = myclass.getAllSymptoms();
foreach (SelectionList listItem in Symptoms)
{
listItem.DataSource = dt;
listItem.DataTextField = "symptomsname";
listItem.DataValueField = "symptomsid";
listItem.DataBind();
listItem.Items.Insert(0, new MobileListItem("None"));
}
And in the update button click event
protected void cmbUpdate_Click(object sender, EventArgs e)
{
foreach (SelectionList listItem in Symptoms)
{
if (listItem.SelectedIndex != 0)
{
cmd.CommandText = "INSERT INTO Patient_Symptom (patientid,symptomid) VALUES (" + UserID + ",'" + listItem.Selection.Value + "')";
cmd.ExecuteNonQuery();
}
}
}
You can try two things. Try placing the databinding code in the PreRender event. The second and better option would be to use an ObjectDataSource controls and bind the control declaratively.
In my code I create the menu items dynamically:
string listClientID = BulletedList1.ClientID.Replace('_', '$');
int counter = 0;
foreach (DataRow dataRow in database.DataTable.Rows)
{
// Add Button
ListItem listItem = new ListItem();
listItem.Value = "buttonItem" + Convert.ToString(dataRow["rank"]);
listItem.Text = " " + Convert.ToString(dataRow["title"]);
listItem.Attributes.Add("onclick", "__doPostBack('" + listClientID + "', '"+ counter.ToString() +"')");
BulletedList1.Items.Add(listItem);
counter++;
}
This menu is inside a update panel:
<div id="MenuItemBox">
<asp:BulletedList
ID="BulletedList1"
runat="server"
OnClick="MenuItem_Click"
>
</asp:BulletedList>
</div>
What I want is when a listitem is clicked it performs a postback. But when I run this, the onclick event is only runned once.
For example. I have 4 listitems. When I click the first item the first time the onclick event is executed. Now I click the second item, the onclick event is also executed. But when I now click the first item again the onclick event is not fired.
When I check the error console in FireFox or Oprah I don't get any errors.
So my question is: how can I fix this and what am I doing wrong?
It seems you need to rebind it after postback.
Where do you add items to the menu and are you checking IsPostBack property?
Please compare html after first loading and postpack to see if _dopostback dissappear.
Then Try to remove IsPostBack check.
My code is working well.
Here is it.
public partial class _Default : System.Web.UI.Page {
protected void Page_Load (object sender, EventArgs e) { }
protected override void OnInit (EventArgs e) {
base.OnInit(e);
//if (!IsPostBack) {
string listClientID = BulletedList1.ClientID.Replace('_', '$');
int counter = 0;
List<SomeClass> items = new List<SomeClass>(){ new SomeClass() { Rank = 1, Title = "2"},
new SomeClass () {Rank = 2, Title = "Two"}};
foreach (var item in items) {
// Add Button
ListItem listItem = new ListItem();
listItem.Value = "buttonItem" + item.Rank;
listItem.Text = " " + item.Title;
listItem.Attributes.Add("onclick", "__doPostBack('" + listClientID + "', '" + counter.ToString() + "')");
BulletedList1.Items.Add(listItem);
counter++;
}
//}
}
protected void MenuItem_Click (object sender, BulletedListEventArgs e) {
Response.Write(e.Index);
}
class SomeClass {
public int Rank;
public string Title;
}
}
Probably UpdatePanel is causing the trouble. I would try pulling it out of UpdatePanel and then would see(which I believe it should) if that works?
Secondly, you can probably just populate the list items Values and Texts only, and then under mnuMainMenu_MenuItemClick(object sender, MenuEventArgs e) event; look for the specific item that has been clicked (e.Item.* public properties), and then probably would use Response.Redirect() to do the job; well, just thinking out loud...