Hi it just so happens that I have to need this kind of approach (or any suggestion if you have)
this code produce table
while (rd.Read())
{
resultsHtml.Append("<tr class='odd gradeX'>");
resultsHtml.Append("<td>" + rd["region_location"].ToString() + "</td>");
resultsHtml.Append("<td>" + rd["region_tag"].ToString() + "</td>");
resultsHtml.Append("<td class='center'>");
resultsHtml.Append("<div class='tooltip-demo'>");
resultsHtml.Append("<a class='btn btn-info' href='regionEdit.aspx?id=" + rd["region_id"].ToString() + "'><i class='fa fa-paw'></i></a> ");
resultsHtml.Append("<asp:Button ID='btnDelete' runat='server' Text='Login' CssClass='btn btn-outline btn-primary' Style='width: 100px;' OnClick='btnDelete_Click' />");
resultsHtml.Append("</div>");
resultsHtml.Append("</td>");
resultsHtml.Append("</tr>");
}
if you will notice there is an asp:button there. because i want that if the user clicks that button, it will just delete it. So I put an
public void btnSubmitDelete_Click(object sender, EventArgs e)
{
}
but the things is. It is not displaying and I can't figure out how to put the region_id in the asp:button to determine which is which to delete and every loop of the while. Please help
i think when u want to add an asp control and show that control to end user u should add dynamically control to a placeholder or panel like this code :
<asp:PlaceHolder runat="server" ID="_plchControles"></asp:PlaceHolder>
and in code bihind :
var btn = new System.Web.UI.WebControls.Button();
btn.Text = "sample";
btn.ID = "btnSubmitDelete";
btn.OnClientClick += btnSubmitDelete_Click;
_plchContent.Controls.Add(btn);
Your script will work as HTML control and you can bind click event from Javascript. To do so define a WebMethod in c# and call it from Javascript click event.
e.g.
$("#btnDelete").on("click",function(e)
{
var para=[]
para.param1=val1;
para.param2=val2;
$.post("/path/delete",para,function(result)
{
});
});
C#
[WebMethod]
public void delete(string param1,string param2)
{
//Delete code here
}
else
mohammadreza izadi answers will work if you want to asp.net control but you have to make object of button as defined not string.
Related
I'm trying to make it so if your Session["LoggedIn"] is true, the wuc (Web User Control, which is the Navbar and is connected to the MasterPage which is connected to the page) prints a logout button.
so I have it like this:
Response.Write("<a runat='server' ID='lblLogout' class='nav-link' CausesValidation='False' OnServerClick='lblLogout_Click'>Logout</a>");
the CodeBehind function looks like this:
protected void lblLogout_Click(object sender, EventArgs e)
{
Session.RemoveAll();
Session["LoggedIn"] = false;
Session["IsAdmin"] = false;
Session["Username"] = "";
}
now, I've tried everything, if I use response.write like that, I can't click it, it just doesn't fire or do anything, if I don't use it inside Response.Write, it does work..
I even tried prinitng an asp:LinkButton instead to see if that works, but it doesn't print out anything when I use it like:
Response.Write("<asp:LinkButton class='nav-link' runat='server' ID='lblLogout' Text='Logout' CausesValidation='False' OnClick='lblLogout_Click' />");
Now the solution that I found was putting another page for logout and placing the function on PageLoad, which works, but I'm wondering if I can make it work so I can use the function from CodeBehind, instead of having to go to another page.
my working solution:
if ((bool)Session["LoggedIn"] == true)
{
Response.Write("<li class='nav-item'> <a runat='server' ID='lblLogout' class='nav-link' CausesValidation='False' href='../PagesForVisitor/wfLogout.aspx'>Logout</a></li><li><a class='navbar-brand' href='#'><img src='" + GetSource() + "' width = '30' height = '30' alt = ''/ ></a></li>");
}
(this one prints out a profile pic as well)
p.s using Bootstrap for styling, not sure if that matters, thanks a bunch..
You should use
<asp:Button id='' runat='server'>
tag
or use css to custom the button look depending on the style.
You can make it a hyperlink in the properties.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a function that edit the content of an aspx page. But the aspx file didn't change at all. I want the aspx file content is also edited automatically.
Here is the function :
protected void add_Click(object sender, EventArgs e)
{
content.InnerHtml = content.InnerHtml + "<br/> <h3>" + headertambahan.Text + "</h3> <p p style=\"font-size:12px; font-weight:normal;\">" + isitambahan.Text + "</p>";
}
What should I do?
You need to either:
1) Make sure the button is causing a postback. Without a postback, no changes will be shown. You can ensure this by checking the properties of your button. Note: If you're using asp:Button this will cause a postback by default. If you're using something else for the click event you'll need to set the property AutoPostBack="true".
2) You could wrap your content object in the markup in an UpdatePanel, and in your button cick call the .Update() method on your UpdatePanel, but that feels like it's probably overkill for what you're doing here.
It sounds like you might want to consider using a literal. Here's an example from W3Schools that, if I understand your question, accomplishes exactly what you are trying to do:
http://www.w3schools.com/aspnet/showaspx.asp?filename=demo_literal2
Also - remember to use runat="server".
Why don't you call a javaScript function on your button click handler that will update your DOM element.
HTML :
<div id="content">
<p>This is nothing</p>
</div>
<asp:Button ID="AddContent" runat="server" OnClick="AddContent_Click" />
JS :
<script type="text/javascript">
function ChangeContent(headertambahan, isitambahan)
{
$('#content').html($('#content').text() + "<br/> <h3>" + headertambahan + "</h3> <p style=\"font-size:12px; font-weight:normal;\">" + isitambahan + "</p>");
}
</script>
Server Side :
protected void AddContent_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "ChangeContent('" + headertambahan.Text + "','" + isitambahan.Text + "');", true);
}
------------------- Solution : 2 (Question Updated Later)
For Permanent Change into your file :
Update the particular section of your page with your desired content. Try this,
HTML :
<div id="content">
<p>This is nothing</p>
<p id='updateBox'></p>
</div>
<asp:Button ID="FileContentUpdate" runat="server" OnClick="FileContentUpdate_Click" />
Server Side :
protected void FileContentUpdate_Click(object sender, EventArgs e)
{
string filePath = #"F:\Stackoverflow\24099577\DomManipulation.aspx";
string[] content = File.ReadAllLines(filePath);
for (int i = 0; i < content.Length; i++)
{
content[i] = content[i].Replace("<p id='updateBox'></p>", "<br/> <h3>" + headertambahan.Text + "</h3> <p style=\"font-size:12px; font-weight:normal;\">" + isitambahan.Text + "</p>");
}
File.WriteAllLines(filePath, content);
Response.Redirect(Request.RawUrl);
}
I am creating an anchor tag on the fly. It gets created, but for some reason the onClick is not working.
Here is my generation code:
HtmlGenericControl emailsubject = new HtmlGenericControl("div");
emailsubject.ID = count++ + "emailSubject";
emailsubject.InnerHtml = "Subject: "
+ "<a id=\"summa\" href=\"#\" onClick=\"subject_Click();\">"
+ results2["EmailSubject"].ToString()
+ "</a>";
Here is my onClick function:
public void subject_Click()
{
Response.Write("Clicked");
}
The way you have it set up right now, you're trying to call a javascript function from an html element. Try either initializing the onclick function of the control you are creating or make use of another asp.net control (ASP:Hyperlink) in order to call functions in code behind.
Here's the thing, I now have a working prototype of what I want and it seems to work perfectly for what I want to do. With that said, this cannot POSSIBLY be the best way to do it. And by best I mean both in terms of program simplicity but also HOW i actually coded it. Anyways first a description of what the page should do:
1) the page contains a textbox (Textbox1), a placeholder (Placeholder1), and a button (Button1)
the code for this is simple:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<hr />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<hr />
2) the user can put a number into the textbox (lets say 99) and click the button. the placeholder will then be filled with 99 textboxes with a "save" button next to each one
3)the user can edit any textbox and click save next to it and the function that is then called by the button click event needs to be able to have access to A) the textboxes current value, B) the textboxes old value, C) any amount of meta data
here is my current working solution:
protected void Page_Load(object sender, EventArgs e)
{
createStuff(false);
}
void test(object sender, EventArgs e)
{
Button btn = (Button)sender;
Response.Write(btn.ID + "Clicked" + "<br />");
Response.Write("Metadata: " + Session[btn.ID + "a"] + "<br />");
Response.Write("Old text: " + Session[btn.ID + "b"] + "<br />");
TextBox txtBlah = (TextBox)PlaceHolder1.FindControl("txt"+btn.ID);
Response.Write("New text: " + txtBlah.Text + "<br />");
Session[btn.ID + "b"] = txtBlah.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["num"] = TextBox1.Text;
createStuff(true);
}
private void createStuff(bool btnClick)
{
PlaceHolder1.Controls.Clear();
int numBtn = Convert.ToInt32(Session["num"]);
for (int i = 0; i < numBtn; i++)
{
TextBox txtTemp = new TextBox();
txtTemp.Text = "old" + i;
txtTemp.ID = "txt" + "btn" + i;
PlaceHolder1.Controls.Add(txtTemp);
Button btn = new Button();
btn.Text = "btn" + i;
btn.ID = "btn" + i;
btn.Click += new EventHandler(test);
PlaceHolder1.Controls.Add(btn);
Session[btn.ID + "a"] = "meta" + i;
if (btnClick)
{
Session[btn.ID + "b"] = txtTemp.Text;
}
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
}
}
I think this is actually pretty decent and I am very happy with it. But, I know there have to be some improvements i can make since some of the tutorials on how the ASP postback system and object model or whatever its called are even now confusing me some.
any help or even just ideas are helpful thanks.
The only major improvement i can see is: use a UserControl, put the TextBox and Button inside, add properties (like OldValue/NewValue, stored in ViewState/TextBox) and add these UserControls to the PlaceHolder after you've created instances by using LoadControl.
The "MetaData" should also be stored as property in the UserControl. You should use Events to communicate between the UserControl and Page(f.e. TextSaved).
You can store the number of created controls in ViewState instead of the Session. That's the only thing you need to store in the Page. Append the index of the UserControl to it's ID.
General advantages of a Web User Control:
The biggest advantage of the Web User controls is that they can be created as a site template and used through out the site. For example they can be made to contain the Menu/Link structure of a site and can be used at all the other aspx pages.
This means the following :
If the website introduces a new site-wide link within the current layout/structure, it is enough if we put it on the user control once. All pages will be updated once if the web user control is used in them.
If there is any link to be corrected, it can be done once at the server side.
The .ascx files can either be used as a simple alternative to the plain HTML or they can also be used to respond to events: This means even custom code can be created against them and put in the code behind files.
http://www.dotnet-guide.com/usercontrol5.html
UserControls encapsulate complexity what is always good to increase
Reusability
Readability
Maintainability
Extensibility
... and to reduce the possibility of errors
Read this SO-answer for more examples.
ok i dunno if this is the right place for me to post back my newest code but i guess it would count as an "answer". also I want to say thank you so much for your answers they have been incredibly helpful. a little cryptic but fifty pages of tutorials or so later and yes your recomendations were fantastic so thank you and bravo. anyways heres my newest one that works perfectly with what I think are your recomended additions. if you want to take a peak and see if youd still do anything a little different or maybe in an easier way please feel free to let me know
first my user control "MyControl"
ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs" Inherits="DBtest.MyControl" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<br />
ascx.cs:
//private String _oldText;
private String _metaData;
public String OldText
{
get
{
//return _oldText;
return (String)ViewState["OldText"];
}
set
{
//_oldText = value;
ViewState["OldText"] = value;
}
}
public String MetaData
{
get
{
return _metaData;
}
set
{
_metaData = value;
}
}
public String NewText
{
get
{
return TextBox1.Text;
}
set
{
TextBox1.Text = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("My ID: " + this.ID + "<br />");
Response.Write("My Metadata: " + _metaData + "<br />");
Response.Write("My Old Text: " + ViewState["OldText"] + "<br />");
Response.Write("My New Text: " + TextBox1.Text + "<br />");
ViewState["OldText"] = TextBox1.Text;
}
then my page, aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="TESTdynamicButton3.aspx.cs" Inherits="DBtest.TESTdynamicButton3" %>
<%# Register TagPrefix="uc" TagName="Spinner" Src="MyControl.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<hr />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<hr />
</asp:Content>
aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
createStuff(false);
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["num"] = TextBox1.Text;
createStuff(true);
}
private void createStuff(bool btnClick)
{
PlaceHolder1.Controls.Clear();
int numBtn = Convert.ToInt32(ViewState["num"]);
for (int i = 0; i < numBtn; i++)
{
MyControl temp2 = LoadControl("MyControl.ascx") as MyControl;
temp2.ID = "uc" + i;
temp2.MetaData = "meta" + i;
//not sure why this DOES work, i would think it is overwritten each page load >.<
temp2.OldText = "old" + i;
if (btnClick)
{
temp2.NewText = "old" + i;
//not sure why this doesnt work below this line
//temp2.OldText = "old" + i;
}
//also not sure why this won't work
//temp2.OldText = temp2.NewText;
PlaceHolder1.Controls.Add(temp2);
}
}
and i also have a couple contents where im not sure it makes perfect sense why or do or dont use these lines to get the "old text" viewstate to populate properly initially. i would have thought i need to have that buttonclick check and only set the viewstate that first time to get it to work. also im not sure i understand why when i tried moving the createStuff() function to OnInit or the pre init functions overridden it wouldnt work..
thanks!
Leo
I have a list in my aspx page where the values for the list are coming from the database. The data can be added to the list successfully
But how can I retrieve the value of the selected list item when I click on that? (ul_Click event)
I don't want to redirect to another page because I'm using AJAX. so i want it in the same page. that's why i commented the
<ul runat="server" id="ulList" onclick="ul_Click">
</ul>
The data is binded to the list in the page_load event.
protected void Page_Load(object sender, EventArgs e)
{
conn = new SqlConnection("Server=NIPUNA-PC\\SQLEXPRESS; Database=XXX; Trusted_Connection=True");
string[] itemList = authorList();
foreach (string item in itemList)
{
HtmlGenericControl newLi = new HtmlGenericControl("li");
//newLi.InnerHtml = "" + item + "";
newLi.InnerText = item;
ulList.Controls.Add(newLi);
}
}
I want the ul_Click() event?
seems like you were on track with the innerHtml thing. You can pass some kind of an ID with the link.
"" + item + "";
then do a request querystring and check for that id. if it's there, then call a function?
i'm not 100% sure, but what you're trying to do might be impossible otehrwise with HtmlGenericControl. I don't think you can bind an onclick event to HtmlGenericControl
Your other option is perhaps go with ajax/javascript
EDIT> just saw the comment. so .... you might do something like
"" + item + "";
you could also do
"" + item + "";
i personally dislike the last one as <a href="#" version because it can cause bad client side behavior such as to go to page top or something stupid like that when clicked.
just to wrap things up. Keep in mind that you'll need a client side javascript to handle the function
<script language="javascript">
function callMyAjaxFunction(bookid){
// do something
}
</script>
adding to robert's ideal instead of including item twice in the markup, you might do
" + item + "
<script language="javascript">
function callMyAjaxFunction(e){
alert(e.innerHTML);// do something
}
</script>