Creating a webpart - please help

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
7/18/2008 10:13:26 AM
Gravatar
Total Posts 9

Creating a webpart - please help

Ok, I posted in the bug forum, but I think that was not correct because I am sure the problem is probably me and not mojoPortal.

here is what I am attempting to do.  I want to create  a ServerControl webpart that has a label, a text box and a button.  This is used to do a database search and just write the results as an HTML table on the page.

So far, I can get the control to do everything I want, except that the text in the textbox is completely ignored.  No matter what I do, I cannot effect the results returned. 

I have checked the SQL code, and I am sure it works (its very simple). 

here is the source code of my webpart.  Ive been trying for a few days, so there is a bit of slop in there..

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data;

namespace MyWebParts
{

public class RecallSearch : WebPart
{
Label lblLabel;
TextBox txtClaimNumber;
Button btnSearch;

private string strSearch = "";

public RecallSearch()
{
this.Title = "Recall Search";
this.Description = "Recall Search";

}

protected override void CreateChildControls()
{
lblLabel = new Label();
txtClaimNumber = new TextBox();
btnSearch = new Button();


lblLabel.Width = 100;
lblLabel.Text = "Claim Number: ";
this.Controls.Add(lblLabel);

txtClaimNumber.Text = "";
this.Controls.Add(txtClaimNumber);

btnSearch.Text = "Search";
this.Controls.Add(btnSearch);

if (Page.IsPostBack)
{
btnSearch.Text = "Search again";
this.strSearch = this.txtClaimNumber.Text;
}


this.ChildControlsCreated = true;

//base.CreateChildControls();
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
EnsureChildControls();
//lblLabel.RenderControl(writer);
//txtClaimNumber.RenderControl(writer);
//btnSearch.RenderControl(writer);

Controls[0].RenderControl(writer);
Controls[1].RenderControl(writer);
Controls[2].RenderControl(writer);

if (Page.IsPostBack)
{
string cnnstr = "server=server;user id=user;database=database;

MySql.Data.MySqlClient.MySqlConnection MyCnn = new MySql.Data.MySqlClient.MySqlConnection();

MyCnn.ConnectionString = cnnstr;

if (MyCnn.State != System.Data.ConnectionState.Open)
{
MyCnn.Open();
}

MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();

cmd.Connection = MyCnn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM tblcontents WHERE txtClaimNumber like '" + this.strSearch +"%'";

MySql.Data.MySqlClient.MySqlDataReader reader;

reader = cmd.ExecuteReader();

writer.Write("<table>");
writer.Write("<tr><td>ID</td><td>Claim Number</td><td>Insured</td></tr>");
while (reader.Read())
{
writer.Write("<tr>");
writer.Write("<td>" + reader[0].ToString() + "</td>");
writer.Write("<td>" + reader["txtClaimNumber"].ToString() + "</td>");
writer.Write("<td>" + reader["txtInsured"].ToString() + "</td>");
writer.Write("</tr>");

}

writer.Write("</table>");

}

//base.Render(writer);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);


}

public override EditorPartCollection CreateEditorParts()
{
EditorPartCollection editorParts = base.CreateEditorParts();

return editorParts;
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}

public override WebPartVerbCollection Verbs
{
get
{
return base.Verbs;
}
}
}

 

7/18/2008 11:07:03 AM
Gravatar
Total Posts 18439

Re: Creating a webpart - please help

You may have better luck trying to use your web part on MyPage where it will have the benefit of a webpartmanager.

Your code listing shows a lot of problems. You should not be doing data access in a web component but should make a business obejct and data layer.

You should look at existing data access code to understand the correct way to get a DataReader using .ExecuteReader. You should use the overload of .ExecuteReader that takes a connection string not a connection and you should make sure and close the reader after databinding and this will automatically close the underlying connection. Your connection is staying open.

Also you are making things harder for yourself by overriding the render method instead of letting it render itself. You should add controls to the control tree in onload and the controls will render themselves in render if you don't override it.

I will see about improving the example SampleWebPart when I get a chance as it probably leads you to believe render is where to do the work but its really not a good example. Hitting the database during render is a bad idea in my opinion, thats too late in the lifecycle of the page/control.

Hope it helps,

Joe

7/18/2008 11:18:24 AM
Gravatar
Total Posts 9

Re: Creating a webpart - please help

Yeah, i was just doing this as a test to see if I could get it working. 

I was actually lead to overriding the render method by just about every webpart example I could find. 

I was thinking that I could accomplish a lot of nifty things by seeing what I could shoehorn into a webpart, but I am starting to believe that is the wrong way to go about this. 

Thank you for your input.

Tony

You must sign in to post in the forums. This thread is closed to new posts.