Asp.net development step (5) customize a repeater template to implement special style controls

Source: Internet
Author: User

 

Recently, a special problem occurred while maintaining the functions of a project. Function dynamic questionnaire for project implementation. That is, dynamically generate a questionnaire. Different types of questionnaires have different questions. Function development has been completed in the early stage. However, the current questionnaire shows a special question style, which is no longer a single choice, multiple choice, or text. It is a composite control with multiple control features, and can be dynamically added in the background. If you write the code according to the user control idea, it feels complicated to implement and the page binding is also troublesome. So I want to simply write a new control with the required features, and then add it directly in the background when there is a control of this type. Add the required attributes (binding and value) to the control 〕. Before writing, it is the foundation for determining the writing. Is it from the beginning? You can also extend it on the basis of existing controls. The control style is as follows:

According to the functional requirements, the general control has the following functions: dynamic binding, a project consists of a check box and a text box.

 

 

 

Finally, we decided to use repeater for expansion. Repeater binding should be efficient and clean. Then there is the option problem.

Define a new repeater template. The above effect is displayed when repeater is dynamically bound.

For template definition code, see:

 

Code

 public class RepeaterTemplate : ITemplate
{

private string _dataValueField;
private string _dataTextField;
private string _textboxField;
public string TextboxField
{
get { return _textboxField; }
set { _textboxField = value; }
}

public RepeaterTemplate(Repeater rpt, string dataValueField, string dataTextField)
{
_dataValueField = dataValueField;
_dataTextField = dataTextField;
rpt.ItemDataBound += new RepeaterItemEventHandler(rep_ItemDataBound);

}

public RepeaterTemplate(Repeater rpt, string dataValueField, string dataTextField,string textboxField)
{
_dataValueField = dataValueField;
_dataTextField = dataTextField;
_textboxField = textboxField;
rpt.ItemDataBound += new RepeaterItemEventHandler(rep_ItemDataBound);

}

public void InstantiateIn(Control container)
{

CheckBox chk = new CheckBox();
chk.ID = "CTCheck";
TextBox tbx = new TextBox();
tbx.ID = "CTText";
HiddenField value = new HiddenField();
value.ID = "hdfValue";

HtmlGenericControl tr = new HtmlGenericControl("tr");
HtmlGenericControl td1 = new HtmlGenericControl("td");
HtmlGenericControl td2 = new HtmlGenericControl("td");

td1.Controls.Add(chk);
td1.Controls.Add(value);
td2.Controls.Add(tbx);
tr.Controls.Add(td1);
tr.Controls.Add(td2);

container.Controls.Add(tr);
}

void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
CheckBox chk = (CheckBox)e.Item.FindControl("CTCheck");
HiddenField value = (HiddenField)e.Item.FindControl("hdfValue");
TextBox tbx= (TextBox)e.Item.FindControl("CTText");
if (chk != null)
{
if (!string.IsNullOrEmpty(_textboxField))
{
tbx.Text = drv[_textboxField].ToString();
if (drv[_textboxField].ToString().Length > 1)
chk.Checked = true;// "Checked";//chk.Checked = true;
}
chk.Text = drv[_dataTextField].ToString();
value.Value = drv[_dataValueField].ToString();
}
}
}

Bind repeater to the encapsulated control.

 

Code

 Repeater rpt = new Repeater ();
Rpt. ID = "rpt ";
Rpt. HeaderTemplate = new HeaderTemplate ();
Rpt. FooterTemplate = new FooterTemplate ();
Rpt. ItemTemplate = new RepeaterTemplate (rpt, field );

Rpt. DataSource = temp;
Rpt. DataBind ();

 

 

 

I want to learn from each other and help some beginners learn how to use repeater. Finally, the program has nothing to think of and cannot do.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.