如何从DetailsView

时间:2017-03-23 17:53:05

标签: c# asp.net .net webforms ascx

我一直在努力解决这个问题。好难过!我ascx的结构就像

<asp:DetailsView ...>
    <Fields>
        .
        .
        <asp:CheckBoxField DataField="ThingEnabled" HeaderText="Thing Enabled"/>
        .
        .
        .
    </Fields>
</asp:DetailsView>

我想要的元素是ThingEnabled

设定:

DetailsView dv = (DetailsView)sender;
CheckBoxField cbf = ????

请注意,CheckBoxField没有ID属性,因此我无法使用FindControl

2 个答案:

答案 0 :(得分:0)

请注意DetailsView使用单元格而不是每行的控件ID,因此您可以使用行,单元格和控件位置获取CheckBoxField值,如下所示:

// Page_Load is just an example event here, change to any event you need
protected void Page_Load(object sender, EventArgs e)
{
    DetailsView dv = sender as DetailsView;

    // the checkbox uses checked state as its value to be passed
    // n = row/cell/control indexes where CheckBoxField has bound into, starting from 0
    // e.g. dv.Rows[0].Cells[0].Controls[0] as CheckBox
    bool checkboxvalue = (dv.Rows[n].Cells[n].Controls[n] as CheckBox).Checked;
}

如果您仍想使用FindControl,请使用ItemTemplate包装器元素并在其上创建CheckBox控件(请注意,您可能需要BoundField使用DataField="ThingEnabled" 1}}在TemplateField之上用于数据绑定):

<asp:DetailsView runat="server" ...>
    <Fields>
        ...
        <asp:TemplateField HeaderText="Thing Enabled">
            <ItemTemplate>
                <asp:CheckBox ID="ThingEnabled" runat="server" Checked="<%# Bind("ThingEnabled") %>">
                </asp:CheckBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Fields>
</asp:DetailsView>

然后,您可以使用FindControl

访问该复选框控件
DetailsView dv = sender as DetailsView;

// n = row/cell indexes, starting from 0
bool checkboxvalue = (dv.Rows[n].Cells[n].FindControl("ThingEnabled") as CheckBox).Checked;

参考/类似问题:

How to get value from DetailsView Control in ASP.NET?

Get the value of a BoundField from a DetailsView

Accessing DetailsView check box value

答案 1 :(得分:0)

也可以使用linq以这种方式获取DetailsView中的任何字段。

app.post('/enduser', (req, res) => {
  let POST_ENDUSER_QUERY =  {
    project_id: req.body.project_id,
    project_type: req.body.project_type,
    status: req.body.status,
    subject: req.body.subject,
    endUser_name: req.body.endUser_name,
    location: req.body.location,
    cp_1: req.body.cp_1,
    cp1_hp: req.body.cp1_hp,
    cp1_jpos: req.body.cp1_jpos,
    cp_2: req.body.cp_1,
    cp2_hp: req.body.cp1_hp,
    cp2_jpos: req.body.cp1_jpos,
    quot_num: req.body.quot_num,
    quot_date: req.body.quot_date,
    note: req.body.note,
    image: req.body.image,
    estimated_value: req.body.estimated_value
  };
  if (!POST_ENDUSER_QUERY) {
    return res.status(400).send({ err: true, message: 'Please project name' });
  }

  let query = `INSERT INTO enduser_tbl (project_id, project_type, status, subject, endUser_name, location, cp_1, cp1_hp, cp1_jpos, cp_2, cp2_hp, cp2_jpos, quot_num,quot_date, note, image, estimated_value) VALUES (
    '${POST_ENDUSER_QUERY.project_id}',
    '${POST_ENDUSER_QUERY.project_type}',
    '${POST_ENDUSER_QUERY.status}',
    '${POST_ENDUSER_QUERY.subject}',
    '${POST_ENDUSER_QUERY.endUser_name}',
    '${POST_ENDUSER_QUERY.location}',
    '${POST_ENDUSER_QUERY.cp_1}',
    '${POST_ENDUSER_QUERY.cp1_hp}',
    '${POST_ENDUSER_QUERY.cp1_jpos}',
    '${POST_ENDUSER_QUERY.cp_2}',
    '${POST_ENDUSER_QUERY.cp2_hp}',
    '${POST_ENDUSER_QUERY.cp2_jpos}',
    '${POST_ENDUSER_QUERY.quot_num}',
    '${POST_ENDUSER_QUERY.quot_date}',
    '${POST_ENDUSER_QUERY.note}',
    '${POST_ENDUSER_QUERY.image}',
    '${POST_ENDUSER_QUERY.estimated_value}'
    )`;
  dbConn.query(query, ( err, results) => {
    if(err) throw err;
    return res.status(200).json({"status": 200, "err" : null, "data": results});
  });
});