cannot change GridView Caption - asp.net

It seems to be impossible to change GridView .Caption after it has been set once.
Once I set caption and then change it within postbacks, in the code all seem to be ok, on page PreRender, GridView PreRender and wherever
I have no idea what to do - on page (and GridView also) PreRender event while debugging the .Caption is proper, but it renders with old caption anyway
Page seems to be render with set-once caption although I changed it.
I even tried to place it to updatePanel and update it, but it didn't help.
Can anybody suggest the reason?
thanks in advance.

Seems to be working here in this example, can you post your code?
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Collections.Generic.List<int> Values = new System.Collections.Generic.List<int> { 1, 2, 3, 4, 5, 6, 7 };
grdTest.DataSource = Values;
grdTest.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
grdTest.Caption = "test grid " + DateTime.Now.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdTest" Caption="test grid" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
hello
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" />
</div>
</form>
</body>
</html>

Related

how to pass server side value to client side after event handler completed ASP.NET?

I have a 'LinkButton' in GridView. when click on it, I want to popup a window. so when I click the linkbutton, onClientClick triggers first. but I want to wait the hidden field value being set in code behind, then pass the hidden value as an arg of my popup window.but now, the hidden field value is always null. So how can I make this work? Thanks.
<%# Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
// load something
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//set hidden feild value
hdfilename.Value = "something";
}
</script>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
function GetSelectedRow(sender) {
if (!sender) return;
var hrefToEvalWhenFinished = sender.attributes['href'].value; // Ext.get(sender).getAttribute('href');
if (hrefToEvalWhenFinished) {
// todo: do async work here
// Fire the links HREF event which will do the postback.
eval(hrefToEvalWhenFinished);
var filename = document.getElementById("<% =hdfilename.ClientID %>").value;
if (filename) {
window.open('/Blank.aspx?f=filename', 'popUpWindow', 'height=500,width=400,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server"
OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkSelect" runat="server" Text='<%# Eval("Description") %>' CommandArgument='<%# Container.DataItemIndex %>' CommandName="ViewDocument" OnClientClick="return GetSelectedRow(this);" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HiddenField ID="hdfilename" runat="server" />
</form>
</body>
</html>

asp LinkButton inside html Button tag

I have some Html code that contains the button tag "<button></button>".
I have noticed that if I place a linkbutton inside this element, the postback of the linkbutton will never occur.
So this is not working :
<button><asp:LinkButton ID="lnkExample" OnClick="lnkExampleBtn_Click"runat="server">Text</asp:LinkButton></button>
The server method "lnkExampleBtn_Click" will never be launched.
How come the postback doesn't work ?
Is there a way to have a linkbutton work inside the "button" tag ?
I also tried putting an href element with javascript _doPostback specific method, but that is not working either.
I've tried to reproduce your case.
Here's code behind:
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnTest_Click(object sender, EventArgs e)
{
lblTest.Text = "Test";
}
protected void btnTest2_Click(object sender, EventArgs e)
{
lblTest.Text = "Test2";
}
}
And here's aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebApplication1.Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="btnTest" runat="server" OnClick="btnTest_Click">Test</asp:LinkButton>
<button><asp:LinkButton ID="btnTest2" runat="server" OnClick="btnTest2_Click">Test2</asp:LinkButton></button>
<button>test</button>
<asp:Label ID="lblTest" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
What I found is - it depends on browser. In Chrome it's possible to place LinkButton inside tags. In Firefox and IE 10 it's not.
The reason why it's not working is simple - it's not allowed by the HTML 5 standard. You just can't embed a link inside a button.
http://www.w3.org/TR/2011/WD-html5-20110525/the-button-element.html#the-button-element
Link inside a button not working in Firefox

HttpUtility.HtmlEncode() do not work in asp.net TextBox control?

My code below:
Test.aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<input type="text" value="<%=HttpUtility.HtmlEncode(ab)%>" runat="server"/>
</form>
</body>
</html>
Test.cs:
public partial class Test: System.Web.UI.Page
{
public string ab;
protected void Page_Load(object sender, EventArgs e)
{
ab = "<script>alert('111');</script>";
}
}
After running the test.aspx page,the textbox value is <%=HttpUtility.HtmlEncode(ab)%>
But remove the runat="server" string show correct!
When you do a runat="server" on a control, it becomes a server control instead of a traditional HTML tag, so the attributes are dealt with on the server. Unfortunately, that means the inline script tags don't always do what you want them to do.
You can do a few things. Either leave off the runat="server" like you said, which makes it render exactly as you want, or set the value in the code:
myTextBox.Value = "whatever";
You can also use data binding, but it's kind of ugly:
<input id="myTextBox" runat="server" type="text"
value='<%# HttpUtility.HtmlEncode("someString") %>' />
protected void Page_Load(object sender, EventArgs e) {
myTextBox.DataBind();
}

automatically generating a list of links

Ok so I am trying to make a list of links in a page that is generated using a foreach and loop as long as there are objects in the list. Here is the code that I use to generate the links:
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["mamlist"] != null)
{
mamlist = (List<mammifere>)Session["mamlist"];
int i = 0;
foreach (mammifere l in mamlist)
{
mamol.InnerHtml += ("<li><a onClick='select("+i+");' >" + l.Nom + "</a></li>");
i++;
}
}
}
}
For some reason, the links are unclickable. I get this:
How can I make links that do not lead to another page but instead launch a method in the C# code of the page?
You can create LinkButton controls that call subroutines/methods in your ASPX code:
Sample code:
<%# Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>LinkButton Example</title>
<script language="C#" runat="server">
void LinkButton_Click(Object sender, EventArgs e)
{
Label1.Text="You clicked the link button";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>LinkButton Example</h3>
<asp:LinkButton id="LinkButton1"
Text="Click Me"
Font-Names="Verdana"
Font-Size="14pt"
OnClick="LinkButton_Click"
runat="server"/>
<br />
<asp:Label id="Label1" runat="server" />
</form>
</body>
</html>
In your specific case, add a ContentPlaceHolder in your master page:
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server" />
In the page where you want the links to appear you add a Content control like so:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
Then foreach link you want, do this:
foreach (mammifere l in mamlist)
{
LinkButton linkButton = new LinkButton();
linkButton.Text = l.Nom;
linkButton.OnClick= "LinkButton_Click";
linkButton.ID = l.Nom;
Content1.Controls.Add(linkButton);
}

asp.net dropdownlist - C#.NET and javascript

I have a drop down and a div right next to it. The text contained inside the div should be updated based on value selected in the dropdown.
I do NOT want to use AutoPostBack feature.
Use OnChange client side event of the dropdownlist control. Here is a sample that updates a div's inner text with the selected value of the dropdown list :
<asp:DropDownList runat="server" ID="myDDL"
OnChange="document.getElementById('myDiv').innerText=this.value;">
</asp:DropDownList>
<div id="myDiv"></div>
As people have said, you cannot access the C# codebehind code without performing a postback.
One way of accomplishing what you want is to use PageMethods. This is the Microsoft idiom for accomplishing this but there are lots of other Ajax libraries that will do it.
Provide a static method in your codebehind which will get called form your dropdownlist OnChange event.
[WebMethod]
public static string MyMethod()
{
// Your code goes here
return "The text for the div"
}
You then call this PageMethod from your .aspx page:
You need to add a scriptmanager (after the form tag)
<asp:ScriptManager ID="scriptmanager1" EnablePageMethods="true" EnablePartialRendering="true" runat="server" ></asp:ScriptManager>
Then in the OnChange event (or a function called from it) you have:
<asp:DropDownList id="ddl" runat="server" OnChange="PageMethods.MyMethod(OnComplete);" />
Where OnComplete is a javascript function that handlers the PageMethod response:
<script language="javascript">
function OnComplete(result, userContext, methodName) {
document.getElementById(yourDivId').innerText=result;
}
</script>
You can also have a paramaterised webmethod if needed:
[WebMethod]
public static string MyMethod(string myNeatParam)
{
// Your code goes here
return "The text for the div"
}
And call it with:
PageMethods.MyMethod('SomeValue', OnComplete)
Updated based on comments:
Here's a sample page on using an UpdatePanel to do what you need to do. Please keep in mind though that there is a full postback happening, even though it is transparent to the user.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UpdatePanelMadness._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ColorsDropDownList" runat="server" AutoPostBack="true"
onselectedindexchanged="ColorsDropDownList_SelectedIndexChanged">
<asp:ListItem Text="Red" Value="Red" />
<asp:ListItem Text="Green" Value="Green" />
<asp:ListItem Text="Blue" Value="Blue" />
</asp:DropDownList>
<div id="ColorDiv" runat="server">
<p>
Hello World!
</p>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
code behind:
namespace UpdatePanelMadness
{
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ColorsDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
ColorDiv.InnerText = this.ColorsDropDownList.SelectedValue;
}
}
}

Resources