简体   繁体   English

此代码 go 在 Web Forms 项目中在哪里?

[英]Where does this code go in a Web Forms project?

I am trying to figure out where to put this code.我想弄清楚在哪里放置这段代码。 I have tried placing it is the default.aspx.cs file but I am not for sure if I am putting it in the wrong portion of the template or not.我尝试将其放置为 default.aspx.cs 文件,但我不确定是否将其放置在模板的错误部分。 Also, I have tried placing the aspx code in the default.aspx folder but I am having the same issue with that as well.另外,我尝试将 aspx 代码放在 default.aspx 文件夹中,但我也遇到了同样的问题。

If someone can show me where this code is supposed to be placed that would be great!如果有人能告诉我这段代码应该放在哪里,那就太好了!

Here is the code that I have:这是我拥有的代码:

aspx code aspx 代码

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

<h1>Northwind Traders</h1>
<hr />
<br />
<div class="col-md-12 table-responsive">
<asp:GridView ID="ProductsViewID" runat="server" OnPreRender="Grid_PreRender"
PageSize="5" CssClass="table table-hover table-condensed" AutoGenerateColumns="True" Style="width: 100%;">
</asp:GridView>

</div>
</asp:Content>

And here is the cs code这是cs代码

public void LoadProductsData()
{
DataTable dt = new DataTable();
//We need to fetch data from Data Base we can call Fetch_ProductList() with
//necessary changes mentioned. Here i am considering MS SQL DB
//since Data base details are not provided i am creating Data Table manually.
  
//Creating columns
dt.Columns.Add("Product");
dt.Columns.Add("First Name");
dt.Columns.Add("Last Name");

//Adding Rows
dt.Rows.Add("Northwind Traders Almonds","Roland","Wacker");
dt.Rows.Add("Northwind Traders Beer","Soo Jung","Lee");
dt.Rows.Add("Northwind Traders Beer","Francisco","Perez-Olaeta");
dt.Rows.Add("Northwind Traders Beer","Karen","Tob");

dt.Rows.Add("Northwind Traders Boysenberry Spread", "Run", "Liu");
dt.Rows.Add("Northwind Traders Boysenberry Spread", "Roland", "Wacker");

dt.Rows.Add("Northwind Traders Cajun Seasoning", "John", "Rodman");
dt.Rows.Add("Northwind Traders Cajun Seasoning", "Roland", "Wacker");

//Assigning Data Table to Gridview
ProductsViewID.DataSource = dt;
//Binding the data to GridView
ProductsViewID.DataBind();
}

protected void Grid_PreRender(object sender, EventArgs e)
{
GridView grid = sender as GridView;
try
{
if (grid != null && grid.Rows.Count > 0)
{

grid.UseAccessibleHeader = true;
grid.HeaderRow.TableSection = TableRowSection.TableHeader;
grid.FooterRow.TableSection = TableRowSection.TableFooter;
}
}
catch (Exception)
{
}

}


public DataTable Fetch_ProductList()
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection("ConnectionString"); //Here we need to provide ConnectionString of Data Base.
SqlCommand cmd = new SqlCommand();
try
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
//Here we need to provide the Query to fetch the data from DataBase
//Sample Query would be 'SELECT PRODUCT_NAME,[FIRST NAME],[LAST NAME] FROM NORTHWIND_TRADERS_TABLE'
cmd.CommandText = "HERE WE NEED TO PROVIDE QUERY";
conn.Open();
SqlDataAdapter reader = new SqlDataAdapter(cmd);
reader.Fill(dt);
conn.Close();
}
catch (Exception ex)
{
conn.Close();
}
return dt;
}

Again, I am just needing help with the placement of the code.同样,我只需要放置代码的帮助。 The code itself should work as intended.代码本身应该按预期工作。 Any help is appreciated!任何帮助表示赞赏!

Web Forms has a couple places for this kind of thing, but probably you should look at the App_Code folder. Web Forms 有几个地方可以处理这种事情,但可能你应该看看App_Code文件夹。 You can put additional *.cs files in there with their own classes defined, including static classes.您可以在其中放置其他*.cs文件并定义它们自己的类,包括 static 类。

Additionally, methods that fill a DataTable should never also directly update controls or set a data source.此外,填充 DataTable 的方法也不应该直接更新控件或设置数据源。 Instead, have the method (in this case LoadProductsData() ) return the DataTable as a value.相反,让方法(在本例中LoadProductsData() )将 DataTable 作为值返回。 Then, the ASP.Net Page Life Cycle event handlers will call into your method and use the result to update the controls on the page.然后,ASP.Net 页面生命周期事件处理程序将调用您的方法并使用结果来更新页面上的控件。

Finally, there's some code here leading me to believe you haven't yet been exposed to Sql Injection.最后,这里有一些代码让我相信你还没有接触过 Sql 注入。 Make sure you are NEVER using string concatenation to substitute user values into an SQL query.确保您从不使用字符串连接将用户值替换为 SQL 查询。 Instead, learn how to use parameterized queries.相反,学习如何使用参数化查询。 It's hard to understate how important this is.很难低估这是多么重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM