我该如何减少代码? [英] How Do I Reduce The Code?
问题描述
目前iam正在开发一个简单的博客网站。
UI是
日期:文本框
消息:文本框
关键字:文本框
主题:文本框
像this.along这些控件代表12个月显示12个按钮,
像Jan, feb,mar,.... 12月。
当我们在控件中输入数据并按下提交按钮时,数据将存储在Database.if中我们希望看到特定的月份数据,只需按下关于月份按钮,现在特定月份的数据将绑定到gridview并在下一页显示此gridview。
i编写特定月份的代码(11月)
protected void november_Click( object sender,EventArgs e)
{
Response.Redirect( Messages.aspx 跨度>);
}
和在Message.aspx中显示gridview中的数据
ds = l2 .nov();
g1.DataSource = ds;
g1.DataBind();
如果在这种情况下,我会编写每个月按钮点击事件的代码代码非常庞大。
是否有任何减少代码的循环。如果请给出解释
请给我回复以减少代码。
您创建一个参数函数,其中您将月份名称传递给函数并根据参数值编写查询,将您的12个按钮功能减少为1个函数
ex。
Jan按钮
ds = l2.funcname( Jan);
g1.DataSource = ds;
g1.DataBind();
Fab按钮
ds = l2.funcname(Feb);
g1.DataSource = ds;
g1.DataBind();
i认为它会帮助你
你需要为所有月份按钮创建一个事件处理程序。请参阅:如何:在ASP.NET网页中创建事件处理程序 [ ^
present iam developing one simple blog site.
the UI is
Date: Textbox
Message:Textbox
Keywords:Textbox
Subject:Textbox
like this.along with these controls displaying 12 buttons on behalfof 12 months,
like Jan,feb,mar,....December.
when we enter the data in controls and press submit button the data will be stored in Database.if we want to see the perticular monthof data,just press the regarding month button ,now the data of perticular month will be binded to gridview and display this gridview in next page.
i write the code for perticular month (November)
protected void november_Click(object sender, EventArgs e)
{
Response.Redirect("Messages.aspx");
}
and in Message.aspx to show the data in gridview
ds = l2.nov();
g1.DataSource = ds;
g1.DataBind();
if in this condition i write the code for every month button click event in the sense the code is very huge.
is there any loop for reducing the code.if yes pls give the explanation
please give me reply to reduce the code.
You create one parameter function where you pass Month name to function and write query based on parameter value its reduce your 12 button fuction to 1 function
ex.
Jan button
ds = l2.funcname("Jan");
g1.DataSource = ds;
g1.DataBind();
Fab button
ds = l2.funcname("Feb");
g1.DataSource = ds;
g1.DataBind();
i think it will help u
You need to create one event handler for all "month-buttons". See: How to: Create Event Handlers in ASP.NET Web Pages[^]
这篇关于我该如何减少代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!