I need some help getting a button into my results that i have SELECTED from a Table. I would like to be able to add a button, so if there was 10 results their would be 10 buttons; 1 in each row. I dont need any onclick events right now as i can sort that later, i just need to position the button, i have tried but it just errors.
Here is the code so far, i have marked where i would like the button.
I get errors like the + is not valid with void or string.
Thanks, PS - im using the latest .NET Framework
PHPRO
February 23rd, 2008, 01:39 PM
Anyone?
I shall go into more detail as that will probably help.
I am wanting to dynamically create buttons; the number of buttons based on the number of results received from an SQL query. For example, 10 rows of information received, then there will be a button for each row.
I have marked where i would like a button, The code for generating a button is in my previous post, although when i add " + InitializeButton() + " it doesent work.
Please help,
Thanks,
PHPRO
February 24th, 2008, 04:36 AM
I think i may need to use a foreach loop for this , i have been testing it words and it seems to be what i am after, although i think the problem is with the Control.Add attribute where it is like this Control.Add(Button1), this way the for each loop it is only adding one button because it can only have one button with that name, so how would i make each button individual? but keep the click propery to be the same, ie if there is 100 results the is not 100 onclick attributes but just the one.
//returns string for a newbutton
//all buttons have the same event callback
//all buttons have a unique name made from the word button and buttonNum
//call this in a for loop passing in the index of the loop
string newButton(DataRow row, int buttonNum)
{
return "<BUTTON type=\"button\" name=\"button\""+buttonNum+"onclick=\"onClickEvent\">"+buttonTextProbablyGottenFromRow+"</BUTTON>";
}
There are 8 (or so) string concatenations in the original version, and zero in the second. The whole point of a stringbuilder is to avoid unnecessary string concatenations. Let the string builder deal with all the concatentations, that's what its good for!
//returns string for a newbutton
//all buttons have the same event callback
//all buttons have a unique name made from the word button and buttonNum
//call this in a for loop passing in the index of the loop
string newButton(DataRow row, int buttonNum)
{
return "<BUTTON type=\"button\" name=\"button\""+buttonNum+"onclick=\"onClickEvent\">"+buttonTextProbablyGottenFromRow+"</BUTTON>";
}
Hello, thanks for the reply,
But, when i replace my code with yours i get errors, i think its due to the { and }'s. where in my Windows Forms Application would i put this code and how would i use it?
Thanks
jon.borchardt
February 24th, 2008, 05:58 PM
I was just offering psudocode
it wont compile as i wrote it
it sounds like you almost had it
you just need to build a button in html, like in the code i sent, instead of in c#, like your code was doing.
the rest looked ok
this code would be placed in the codebehind and called during some kind of postback to create the html page...
and yes... use a string builder....
PHPRO
February 25th, 2008, 01:04 PM
I was just offering psudocode
it wont compile as i wrote it
it sounds like you almost had it
you just need to build a button in html, like in the code i sent, instead of in c#, like your code was doing.
the rest looked ok
this code would be placed in the codebehind and called during some kind of postback to create the html page...
and yes... use a string builder....
Yes that's it! thank you so much!, i used html in the webbrowser control on my Win Form, however i have my click command made up but i have never created an click command that is the result of an html button, only the windows.button. How would i do this?
jon.borchardt
February 25th, 2008, 01:08 PM
Do you need to use the webcontrol?
this is easy to do in winforms
if you have to do web
you either need to add a javascript for the buttons to call... onClikc="javascriptMethodName"
or
you need to use a button with a runat="server" attribute so it can access an onclick method in c#.
Can you give more clarity on what your requirements are, so we can better help?
PHPRO
February 25th, 2008, 01:28 PM
What the webBrowser in my form does is select information form my MySQL database and print it all out, there is a url that is selected from the database (a file the user can download). This is then put into a global variable. The button in every row should open a new form, where i then proccess the information in the url and download the file.
Why are you using the webbrowser to display data, rather than just a form?
Can the whole ui be in a form, and no webbrowser?
Can we just but buttons in the form, instead of in the webBrowser?
PHPRO
February 25th, 2008, 01:40 PM
Why are you using the webbrowser to display data, rather than just a form?
Can the whole ui be in a form, and no webbrowser?
Can we just but buttons in the form, instead of in the webBrowser?
I dont know how to use datagrids with mysql though, that is why i used the webBrowser. Is there a way to do this without the webBrowser?
Thank You,
jon.borchardt
February 25th, 2008, 01:46 PM
yes
psudocode...
get datareader from mysql connection
for each row in datareader
{
create a c# button
set its event delegate
set its position
add it to the form
}
then, on the form
create an onClick event for the buttons to reference
i can write this up, but i don't want to do it if there is some reason you need a webbrowser
if you can give me a datareader, i can give you a form...
PHPRO
February 25th, 2008, 01:51 PM
Hello,
Thank you very much, this code connects to the database and gets the information.
MySqlConnection myConnection = new MySqlConnection();
myConnection.ConnectionString = "*************";
myConnection.Open();
string mySelectQuery = "SELECT * FROM table";
MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection);
MySqlDataReader myReader;
myReader = myCommand.ExecuteReader();
Then currently its a while loop like so
while (myReader.Read())
{
So i would need to change this to a foreach?
Could you provide an example of how i would print the data out onto the form and not onto the webBrowser?
Thanks very much,
jon.borchardt
February 25th, 2008, 02:09 PM
This may or may not compile... but it will give you the idea
System.Drawing.Point butonPos = new System.Drawing.Point(0,0);
int index = 0;
while(myReader.Read())
{
//using the data from the reader create a button
Button button = new Button();
button.Text = dr.GetString(0);//or wherever the data is in your reader
button.Tag = "put whatever data here, as an object, you need to pass to event";
button.Click+=new EventHandler(button_Click);// assign event
this.Controls.Add(button); //add button to form
button.Location = butonPos;
//if you also want to create other info on the form, now is the time to do it
//example
Label lab = new Label();
lab.Text = "yo" + index++;
this.Controls.Add(lab); //add button to form
lab.Location = new Point(200, buttonPos.Y);
butonPos.Y += button_Click.Height * 1.5;//set place for next button to create
}
}
void button_Click(object sender, EventArgs e)
{
Button senderButton = (Button)sender;
string buttonTagString = (string)senderButton.Tag; <--- this is how you can GetChildAtPointSkip data from the button