当所选的下拉列表更改时,更改当前行的名称列文本

时间:2015-06-25 06:16:13

标签: c# asp.net

你好我跟随了this link的泥鳅,我几乎无需帮助。

当选定的下拉列表更改时,如何更改当前行的名称列文本?

例如:第一列是文本第二列是下拉列表,当我更改下拉列表时,我想用一些像这样的值替换文本

gridview1.rows[currentrowindex.cells[0].text="xyz"

这是我的aspx文件

<form id="form11" runat="server">
<div>
    <asp:Label ID="Label1" runat="server" Text=" "></asp:Label>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound ="GridView1_RowDataBound">
        <Columns>
            <asp:BoundField DataField="input_metadata_id" HeaderText="input metadata id" ReadOnly="True" SortExpression="input_metadata_id" Visible="false"></asp:BoundField>
            <asp:BoundField DataField="input_id" HeaderText="input id" ReadOnly="True" SortExpression="input_id"></asp:BoundField>
            <asp:TemplateField HeaderText = "field">
                <ItemTemplate>
                    <asp:Label ID="lblfield" runat="server" Text=""/>
                    <asp:DropDownList ID="selectfield" runat="server"  AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                        </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="datatype" HeaderText="datatype" ReadOnly="True" SortExpression="datatype"></asp:BoundField>
            <asp:BoundField DataField="size" HeaderText="size" ReadOnly="True" SortExpression="size"></asp:BoundField>
            <asp:BoundField DataField="unit" HeaderText="unit" ReadOnly="True" SortExpression="unit"></asp:BoundField>
        </Columns>
    </asp:GridView>
    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" >
        <Columns>
            <asp:BoundField DataField="output_metadata_id" HeaderText="output metadata id" ReadOnly="True" SortExpression="output_metadata_id"></asp:BoundField>
            <asp:BoundField DataField="output_id" HeaderText="output id" ReadOnly="True" SortExpression="output_id"></asp:BoundField>
            <asp:BoundField DataField="field" HeaderText="field" ReadOnly="True" SortExpression="field"></asp:BoundField>
            <asp:BoundField DataField="datatype" HeaderText="datatype" ReadOnly="True" SortExpression="datatype"></asp:BoundField>
            <asp:BoundField DataField="size" HeaderText="size" ReadOnly="True" SortExpression="size"></asp:BoundField>
            <asp:BoundField DataField="unit" HeaderText="unit" ReadOnly="True" SortExpression="unit"></asp:BoundField>
        </Columns>
    </asp:GridView>
</div>
</form>

文件背后的代码

protected void Page_Load(object sender, EventArgs e)
{
SetInputTableInitialRow();        
load_output_table();
}


DataTable outputtable;

private void load_output_table()
{
   // int output_id = 2;
    String query = "select * from output_metadata";// where output_id=" + output_id;
    outputtable = DataAccess.select(query);
    GridView2.DataSource = outputtable;
    GridView2.DataBind();
    int i = 0;

}

private void SetInputTableInitialRow()
    {
        DataTable dt = new DataTable();

        //Define the Columns
        dt.Columns.Add(new DataColumn("input_metadata_id", typeof(int)));
        dt.Columns.Add(new DataColumn("input_id", typeof(int)));
        dt.Columns.Add(new DataColumn("datatype", typeof(string)));
        dt.Columns.Add(new DataColumn("size", typeof(string)));
        dt.Columns.Add(new DataColumn("unit", typeof(string)));
        DataRow dr = dt.NewRow();

        dt.Rows.Add(dr);
        GridView1.DataSource = dt;
        GridView1.DataBind();
     }
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList drp = (DropDownList)sender;
    GridViewRow gv = (GridViewRow)drp.NamingContainer;
    int index = gv.RowIndex;
    Label label1 = (Label)GridView1.Rows[index].FindControl("lblfield");
    label1.Text="abc";

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Find the DropDownList in the Row
        DropDownList selectfield = (e.Row.FindControl("selectfield") as DropDownList);
        String comboboxquery = "select field from input_metadata";
        DataTable table = DataAccess.select(comboboxquery);
        selectfield.DataSource = table;
        selectfield.DataTextField = "field";
        selectfield.DataValueField = "field";
        selectfield.DataBind();
        //Add Default Item in the DropDownList
        selectfield.Items.Insert(0, new ListItem("Please select"));
    }
}

when i select something on dropdown i want to put sum text in the column cells with db values i fetch

2 个答案:

答案 0 :(得分:1)

试试这个

gridview1.rows[currentrowindex].cells[0].Value = "xyz";

修改

在此下拉列表中设置AutoPostBack="true"

<asp:DropDownList ID="selectfield" runat="server"  AppendDataBoundItems="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>

同时在DropDownList1_SelectedIndexChanged方法中更改此行。

Label label1 = (Label)GridView1.Rows[index].FindControl("lblfield");

您正在通过身份Label1找到控件,而其ID为lblfield

答案 1 :(得分:0)

中添加page_load的内容
[HttpGet]
[Authorize(Roles="Admin")]
public ActionResult AuthorizedView() {
    return View("~/ng-app/admin_template.cshtml");
}