|
so so, as you can use the template column or ItemDataBound event instead and you no need to walk through again the Items collection of the grid to dislay the image.
|
|
|
|
|
Is there any way to override page constructor in ASP. NET?
I am using vs2005 and when I try to run the page with overriden constructor I get an error "Object reference not set to an instance of an object." for the control I'm trying to use within constructor. If I try to add this control programmatically I get an error that it already exsists. What can I do to make this work???
<br />
public partial class _Default : System.Web.UI.Page<br />
{<br />
public _Default()<br />
{<br />
<br />
RadioButton b = new RadioButton();<br />
b.ID = "e";<br />
Panel1.Controls.Add(b);
}<br />
<br />
}<br />
--Nikola--
modified 7-Dec-20 21:01pm.
|
|
|
|
|
Hi there,
You cannot get reference to the Panel control at this stage as it's not initialized yet. There are a couple of ways to dynamically add the control, for example you can use the Page_Init event or override the OnInit method .... for more information, see ASP.NET Page Life Cycle Overview [^]
|
|
|
|
|
I have tried that but when I add a control within OnInit or PageLoad I can not access it within event handler. All programatically added controls are lost.
It goes like this:
I have a page with a button and I add some radio buttons programatically:
<br />
Int32 i=0;<br />
for (i=0;i<5;i++)<br />
{<br />
RadioButton b = new RadioButton();<br />
b.ID = "radio_" + i.ToString();<br />
Controls.Add(b);<br />
}<br />
and in the event handler for the button click:
<br />
RadioButton b = FindControl("radio_2"); <br />
FindControll returns null.
I'm a bit confused so if you have any ideas it would be great.
Thanks.
P.S. This is simplified code of what I'm trying to do but the point is the same.
--Nikola--
modified 7-Dec-20 21:01pm.
|
|
|
|
|
The FindControl method basically looks for a specific control within the scope of the current naming container. For example, if you call Panel1.FindControl("Radio1") , the method only searches the Panel1 control's control tree for the Radio1 . In other words, you need to get reference to the naming container of the dynamic controls before using the FindControl method. For more information, see Accessing ASP.NET Controls Programmatically [^]
|
|
|
|
|
My problem is not FindControl my problem is that when a ButtonClick event raises all programatically added controls are lost. If ,based on your example, I use panel to add controls inside event handler Panel1.Controls.Count is 0.
--Nikola--
modified 7-Dec-20 21:01pm.
|
|
|
|
|
Nikola Knezevic wrote: my problem is that when a ButtonClick event raises all programatically added controls are lost
Okay, you didn't mention that in your previous posts. Are you sure that you always readd the dynamic controls to the web page on postback as the ASP.NET does not persist that for you. If you don't, they get lost. If this is not the case, could you post more code snippets?
|
|
|
|
|
Ok here it is.
this portion of code is in PageLoad:
<br />
if(!IsPostBack)<br />
{<br />
while (rd3.Read())<br />
{<br />
TableRow r1 = new TableRow();<br />
TableCell c1 = new TableCell();<br />
TableCell c2 = new TableCell();<br />
TableCell c3 = new TableCell();<br />
<br />
RadioButton rb = new RadioButton();<br />
rb.GroupName = "group_" + Session["questionID"];<br />
rb.ID = "Q_" + i + "_A_" + j;<br />
rb.Attributes.Add("dbID", rd3["AnswerID"].ToString());<br />
c1.Controls.Add(rb);<br />
j++;<br />
<br />
Label lbAnswer = new Label();<br />
lbAnswer.Text = rd3["Text"].ToString();<br />
<br />
<br />
c2.Controls.Add(lbAnswer);<br />
<br />
if (rd3["AnswerPicture"] != null)<br />
{<br />
Image im = new Image();<br />
im.ImageUrl = rd3["AnswerPicture"].ToString();<br />
if (im.ImageUrl.Length > 3)<br />
c3.Controls.Add(im);<br />
}<br />
<br />
<br />
r1.Cells.Add(c1);<br />
r1.Cells.Add(c2);<br />
r1.Cells.Add(c3);<br />
<br />
tblQuestions.Rows.Add(r1);<br />
<br />
}<br />
<br />
}
else<br />
i -= 1;<br />
newPage = false;<br />
}
}<br />
and this method is called in event handler for the "Next" button
<br />
private void CheckAnswers()<br />
{<br />
Response.Write(Session["areaID"]);<br />
Int32 i = 0;<br />
while (true)<br />
{<br />
<br />
<br />
Label lbl = (Label) tblQuestions.Rows[i].Cells[1].FindControl("Q_" + i);<br />
<br />
if (lbl == null)<br />
{<br />
break;<br />
}<br />
<br />
Int32 j = 0;<br />
while (true)<br />
{<br />
RadioButton rb = (RadioButton) tblQuestions.Rows[i].Cells[0].FindControl("Q_" + i + "_A_" + j); <br />
<br />
if (rb == null)<br />
{<br />
break;<br />
}<br />
<br />
if (rb.Checked)<br />
{<br />
<br />
if (isCorrect(lbl.Attributes["dbID"], rb.Attributes["dbID"]))<br />
{<br />
Int32 pts = Convert.ToInt32(Session["points"].ToString()) + Convert.ToInt32(lbl.Attributes["pts"]);<br />
Session["points"] = pts;<br />
}<br />
}<br />
<br />
j++;<br />
<br />
}<br />
i++;<br />
<br />
}<br />
}<br />
but it finds no radio buttons
What I need is to load questions from the database, put them on the page and when user klicks "next" to read which radio buttons user clicked and calculate the points.
--Nikola--
modified 7-Dec-20 21:01pm.
|
|
|
|
|
As I said in the above reply that you need to make sure you readd the dynamic controls on postback and the ASP.NET does not do that for you. Now go back to your sample code in the Page_Load event handler, you are checking the if(!IsPostBack) and adding the dynamic radios in the if statement body. That also means this code only executes just one time when the page is first visited, and when you hit the Next button to cause the postback, this code does not execute due to the code if(!IsPostBack) . As a result of that, the dynamic radios do not get reinserted to the web page, and you find no thing in the CheckAnswers. If you still don't get it, you can try stepping through your code in debog mode and see how the Page_Load executes. So the simple fix is that you don't check the IsPostback in the Page_Load.
|
|
|
|
|
I have already tried without if(!IsPostBack) and all the radios are there but none is checked. I guess that by recreating them I recreate their checked state too.
--Nikola--
modified 7-Dec-20 21:01pm.
|
|
|
|
|
Normally, there are a couple of reasons that causes the ViewState of a control to get lost:
+ The control has the ViewState disabled.
+ The control is dynamically readded after the ViewState is loaded.
+ The control has different ID when the ViewState is loaded. For example, when the ViewState is saved the control'ID is "Radio1", but when the ViewState is loaded on postback the value of the ID is "Radio2".
Below is a working example:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder runat="server" ID="PlaceHolderControl1"></asp:PlaceHolder>
<br />
<asp:Button runat="server" ID="btnPushMe" Text="Pushed Me!" OnClick="btnPushMe_Click" />
</div>
</form>
</body>
</html>
//Code-behind
public partial class Default1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Table table = new Table();
for (int rowIndex = 0; rowIndex < 3; rowIndex++)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
RadioButton radio = new RadioButton();
radio.ID = "Radio_" + rowIndex.ToString();
radio.GroupName = "RadioGroup";
cell.Controls.Add(radio);
row.Cells.Add(cell);
table.Rows.Add(row);
}
PlaceHolderControl1.Controls.Add(table);
}
protected void btnPushMe_Click(object sender, EventArgs e)
{
for (int rowIndex = 0; rowIndex < 3; rowIndex++)
{
RadioButton radio = (RadioButton)PlaceHolderControl1.FindControl("Radio_" + rowIndex.ToString());
Response.Write(string.Format("Radio {0}'s checked state : {1} <br>", rowIndex, radio.Checked));
}
}
}
|
|
|
|
|
the numeric pages on my datagrid is not working properly if for example i am displaying pages 1 to 10 but there are 90 pages
there is an option to choose "..." which then displays the other pages numbers but when i try to choose one of these numbers for example page 13 it carry's me right back to page 3 instead of page 13
kenny
|
|
|
|
|
Could you post some of your code snippets?
|
|
|
|
|
I have the following routine:
public static string readHtmlPage(string url) {
string result;
WebRequest request = WebRequest.Create(url);
HttpWebResponse response =(HttpWebResponse) request.GetResponse();
Stream dataStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(dataStream)) {
result = reader.ReadToEnd();
reader.Close();
}
return result;
}
I email the "result" as the body of an email message but it is loosing its CSS formating. It seems that CSS is never applied can anybody detect something wrong?
|
|
|
|
|
The path to the css is probably relative. As what you have sent in the mail is no longer a page, and has no url, the mail program don't know from where it should load the css.
If you specify a complete url for the css file, it has a possibility to work.
---
b { font-weight: normal; }
|
|
|
|
|
Hm. All of the above is true. So what would return back the complete page?
|
|
|
|
|
Either change the page, or change the contents once you have read the page.
If you have other resources, like images, their addresses have to be changed also. Some resources won't work even if you supply a correct address, like javascript includes.
There is no way that you can keep all functionality when you send the code of a page as a mail. A mail program is not a browser, so it will not support everything that a browser does.
---
b { font-weight: normal; }
|
|
|
|
|
Hi..
How could I confirm user registration by email?
Some websites tell the user that he should Activate his Account
(When he register) By the email he will recieve.
How could I make a similar thread?
I am too late but i will never give up
|
|
|
|
|
This has many way,
i.e : you can send a email to user contain a link whith his userID.
http://www.sample.com/confirm.aspx?userID=150
when user clicked link in this page you can varify userID and active it.
|
|
|
|
|
Thank you very much ..I hope I find an article for this subject.
I am too late but i will never give up
|
|
|
|
|
Hello,
I posted a message here before and I dont now if this is the right forum for me byt the question that I need help with is this.
I have this code. This code shown all the image file I have but I need to rean in the textfile to.
<%
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(Server.MapPath("/test/"))
Set fc = f.Files
For Each f1 in fc
if right(lcase(f1.name),4) = ".gif" then Response.Write(" ")
Next
%>
And I want to read in a textfile with the same namn but is not a *.gif its a *.txt file. This is the response I get.
foreach (string s in System.IO.Directory.GetFiles(@"c:\dir", "*.gif"))
{
string txtPath = s.Substring(0, s.Length - 3) + "txt";
System.IO.TextReader tr = new System.IO.StreamReader(txtPath);
string text = tr.ReadToEnd();
tr.Close();
}
Now how can I make this work. I need to show the image and then the text after the image. There can be many image and text files so I dont now how I should do it. Is there a king on asp that can help med with this?
|
|
|
|
|
Hi there,
Looks like you want to convert the .net code of reading text files into vb script and put it inside the For Each statement of the classic ASP code. The documentation of the FileSystemObject may give you some hints:
FileSystemObject Sample Code [^]
Also, the Web Development[^] is the right room for the classic ASP questions, and here is for the ASP.NET.
|
|
|
|
|
Hello,
I am new to XML Web Services in .NET. I am looking for some of the following few FREE web services which I can use on my website.
Please suggest me some websites where I can get some of these free webservices.
(1)Sending SMS to anywhere in India
(2)Collecting weather information for Indian Cities and Towns
(3)Checking if an e-mail id is valid or not
(4)Any othere utility webservices
Thanks
-Sudhakar
|
|
|
|
|
|